The statement of the question is wrong. find
does not return Collection
, returns the Model
.
And it’s easy to solve the problem if you’re using a variable and saving the result of the query in it.
Behold:
$user = User::find(1);
$user->update(...);
dd($user); // Valor atualizado
With the invention of function tap
in the newer versions of Laravel, you could do so:
return tap(User::find(1))->update([
'name' => $name,
'age' => $age,
]);
Now, if your question is regarding update
that is returned from Query Builder, you really need to do another query, since the update is not done on top of each Collection item, but directly in the database.
Example:
$query = User::where(['x' => 'y']);
$query->update(['z' => 'x']);
$users = $query->get();
Excellent Wallace, thank you very much! I learned a lot from your reply!
– David Dias