How can I update multiple fields in an Eloquent model? Let’s say I got it like this:
$user = User::where("username", "=", "rok");
And then I have all these model parameters:
$new_user_data = array("email" => "rok@rok.com", "is_superuser" => 1, ...);
I can’t just do:
$user->update($new_user_data);
What’s the proper way? I hope not a foreach.
The following does work, however. Is this the way to go?
User::where("id", "=", $user->id)->update($new_user_data);
The problem with the last one (besides it being clunky) is that when using it from an object context, the updated fields are not visible in the $this variable.
The method you’re looking for is
fill():Actually, you could do
$user->fill($new_user_data)->save();but I find the separate statements a little easier to read and debug.