Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get data after insert/update

Tags:

php

laravel

I have this part of code:

$seller = Seller::where('tel', '=', $request->tel)->where('confirmed', '=', 1)->first();

if($seller){
    $seller = Seller::where('tel',  $request->tel)->update($input); //update
    return response()->json(['message'=>'Seller Updated successfully!', 'data'=> $seller], 200);
} else {
    $seller = Seller::create($input); //create
    return response()->json(['message'=>'Seller created successfully!', 'data'=> $seller], 200);
}

Now want to return inserted or updated data in response, but after google and see lot of posts, all tried are failed, how can I do this? without last inserted id

but return boolean:

{
    "message": "Seller updated successfully!",
    "data": 1
}

How to get last insert id in Eloquent ORM laravel

Most topics want to return id but I want all data.

like image 443
tour travel Avatar asked Jun 30 '26 22:06

tour travel


2 Answers

The key to your problem is understanding 3 Eloquent methods.

  • create() - Returns the inserted model instance.
  • update() - Returns a boolean (0 or 1) based on the success of the update statement.
  • refresh() - Refreshes a model instance by requerying the database.

As in the full example below, you need to treat create() and update() differently since they return different things.

For the create() you can leave it as-is. But the update() needs to be modified to not reassign the $seller variable.

That will stop any errors, but it will return the old data. In order to return the new data, you need to refresh the model (refresh()).

$seller = Seller::where('tel', '=', $request->tel)->where('confirmed', '=', 1)->first();

if ($seller){
    // DO NOT reassign since update() returns boolean
    Seller::where('tel',  $request->tel)->update($input); //update
    
    $seller->refresh(); // Refresh it here
    return response()->json(['message'=>'Seller Updated successfully!', 'data'=> $seller], 200);
} else {
    // This one is good since create() returns newly created model.
    $seller = Seller::create($input); //create
    return response()->json(['message'=>'Seller created successfully!', 'data'=> $seller], 200);
}
like image 68
Brian Thompson Avatar answered Jul 02 '26 11:07

Brian Thompson


This is because you are overwriting your var $sellers with the response of the update method. And update() returns a boolean. Therefore, you don't have to assign the return value to the $seller var in the place of the update.

To return the updated seller model, use the fresh() method in your response. $seller->fresh().

$seller = Seller::where('tel', '=', $request->tel)->where('confirmed', '=', 1)->first();

if ($seller){
    Seller::where('tel',  $request->tel)->update($input); //update
    return response()->json([
        'message' => 'Seller Updated successfully!', 
        'data'=> $seller->fresh()
      ], 200);
} else {
    $seller = Seller::create($input); //create
    return response()->json(['message' => 'Seller created successfully!', 'data'=> $seller], 200);
}
like image 38
Maik Lowrey Avatar answered Jul 02 '26 10:07

Maik Lowrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!