Return Collection after update()?
Asked Answered
C

8

20

Using Raw, how to return collection of updated row?

For example:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

I was expecting dd($updated) to return updated row of collection but it returned 1.

{{$updated->votes}} should return 123
Chime answered 29/3, 2017 at 12:47 Comment(2)
Updated is the no. of rows updated by this queryMalloy
You gonna have to select the row after update statementInhalation
F
20

That's not how it works. You can't expect this query will return you an object:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

If you want to use Query Builder only as you mentioned in your question, you'll need to get an object manually:

$data = DB::table('users')->where('id', 1)->first();

With Eloquent you can use the updateOrCreate():

$data = User::where('id', 1)->updateOrCreate(['votes' => 123]);

This will return an object. update() will return boolean, so you can't use it here.

Fribble answered 29/3, 2017 at 12:59 Comment(2)
huh. updateOrCreate() doesn't seem to work that way per their docs or implementation. did that work in a super old version?Pennywise
updateOrCreate() works for me thanks :)Leis
U
41

Try this

$updated = tap(DB::table('users')->where('id', 1))
    ->update(['votes' => 123])
    ->first();

Unquote answered 30/5, 2020 at 8:2 Comment(7)
The best answer!Shortcircuit
This is it! THXDietetics
Simplest and best answer, can you tell, what Tap() function do here ?Fordone
laravel not working in orm :(Aldus
Laravel 8.* working.Depolarize
Really a good solution :)Glenn
@Fordone The tap method is typically used for executing a callback on a value, but it doesn't return the modified value. If you want to retrieve the updated user record, you should use first after the update method.Inhume
F
20

That's not how it works. You can't expect this query will return you an object:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

If you want to use Query Builder only as you mentioned in your question, you'll need to get an object manually:

$data = DB::table('users')->where('id', 1)->first();

With Eloquent you can use the updateOrCreate():

$data = User::where('id', 1)->updateOrCreate(['votes' => 123]);

This will return an object. update() will return boolean, so you can't use it here.

Fribble answered 29/3, 2017 at 12:59 Comment(2)
huh. updateOrCreate() doesn't seem to work that way per their docs or implementation. did that work in a super old version?Pennywise
updateOrCreate() works for me thanks :)Leis
M
9

for version 6: another way with eloquent to return the new updated model by chaining the update method call through tap :

$user = tap($user)->update(['votes' => 123]);
Megacycle answered 27/2, 2020 at 15:15 Comment(2)
Doing this in laravel 7, I get the model instance of before the update and not afterUndertow
it was working with laravel 6, I am gonna update the answer to limit the answer for version 6Megacycle
S
1

This also returns the updated row:

$user = $user->fill(['votes' => 123])->save();
Substantialize answered 4/2, 2021 at 12:55 Comment(0)
B
1

It's very easy with Eloquent ORM

tap(User::findOrFail(1))->update(["votes" => 123]);

tap is a simple PHP function that basically does:

function tap($value, $callback)
{
   $callback($value);

   return $value;
}
Buttery answered 27/2 at 12:13 Comment(0)
R
0

This trick worked for me:

$updated = DB::select("UPDATE users SET votes = 123 WHERE id = 1 RETURNING votes");

Then, since select returns an array: $votes = $updated[0]->votes;

And I believe you could even retrieve the entire row(s) using RETURNING *

This also work with prepared statements:

$updated = DB::select("UPDATE users SET votes = ? WHERE id = ? RETURNING votes", [$votes, $id]);

IMPORTANT NOTE

I am using a Postgres database. This will work only on a db that supports such a thing as RETURNING

Ratal answered 11/8, 2022 at 8:37 Comment(0)
C
-1

you get the first row again after update see example bellow

$user = User::where('id', 1);
$userOld = $user->first(); // will return the first row
$isUserUpdated = $user->update(['name'=>'new name']); // will return true or false 
$updatedUser = $user->first(); // now it will return you the latest updated data

by this example you have old data and new data and is data updated result, now we can return new data.

return response()->json(['status' => $isUserUpdated,'data'=>$updatedUser], 200);
Congreve answered 26/9, 2020 at 17:36 Comment(1)
Its working for me, nicely.Infantryman
K
-12

In controller you write below code for update :

 $updated = DB::table('users')->where('id', 1)->update(['votes' => 123])->get();
Killie answered 29/3, 2017 at 13:3 Comment(1)
update() returns an integer, not a query builder objectHentrich

© 2022 - 2024 — McMap. All rights reserved.