Kousha
Kousha

Reputation: 36299

Laravel Eloquent - update() function

Laravel's Eloquent update() function returns a boolean, and not the updated entry. I'm using:

return User::find($id)->update( Input::all() )

And this returns only a boolean. Is there a way I can get the actual row, without running another query?

Upvotes: 0

Views: 7811

Answers (2)

Back2Lobby
Back2Lobby

Reputation: 593

Another approach can is to use the laravel tap helper function.

Here is an example to use it for updating and getting the updated model:

$user = User::first();
$updated = tap($user)->update([
    "username" => "newUserName123"
]);

tap($user) allows us to chain any method from that model. While at the end, it will return the $user model instead of what update method returned.

Upvotes: 2

fmgonzalez
fmgonzalez

Reputation: 823

I think that's the behaviour that you want:

$user = User::find($id)->fill(Input::all());
return ($user->update())?$user:false; 

I hope it works fine for you.

Upvotes: 5

Related Questions