Home > Enterprise >  Laravel get data after insert/update
Laravel get data after insert/update

Time:12-14

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.

CodePudding user response:

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 query.
  • 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 (fresh()).

$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);
}

CodePudding user response:

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);
}

CodePudding user response:

You can use fresh method

  $seller = Seller::where('tel',  $request->tel)->update($input)->fresh()

This will select fresh set from database

  • Related