0

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You sir are a saviour! Is there a way to use fill() by passing the relationship data as well and then use push() to update all?
I had changes on the base Model class, and I made a kind of preprocess the Input before save/update. I split the related input data and save/update it all too inside the same transaction, but Eloquent doesn't do it by default.
Okay, so that in the case, I'll do something similar
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.