3

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.

Jack The Baker
  • 1,781
  • 1
  • 20
  • 51
  • You're treating `update` and `create` like they are the same, but they are not (https://laravel.com/docs/8.x/eloquent#inserts). `create` returns the new model like you want, but `update` returns a boolean based on the success of the update. – Brian Thompson Dec 13 '21 at 15:33
  • In the update case, you aparently already have a `$seller`, so just don't reassign it – Brian Thompson Dec 13 '21 at 15:33
  • @BrianThompson No actually. in update case, when a seller already exists but not confirmed own phone number, so we let someone else register it. `$seller = Seller::where('tel', '=', $request->tel)->where('confirmed', '=', 1)->first()` – Jack The Baker Dec 13 '21 at 15:37
  • I'm not sure what that means in the context of this problem. The problem is your response says the data is `1` because that's what `update()` returns when successful. If you need to make sure you return the updated data, then **after** the update method (without assigning it to `$seller`) you can call `$seller->fresh()` before returning it. – Brian Thompson Dec 13 '21 at 15:39
  • @BrianThompson Actually I want return created or updated data in response thats all, I alraedy tested `fresh()` but return error – Jack The Baker Dec 13 '21 at 15:42
  • 1
    I'm adding an answer so I have more room to explain. I don't think you're following all of the parts that are required. – Brian Thompson Dec 13 '21 at 15:43

3 Answers3

3

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);
}
Brian Thompson
  • 13,263
  • 4
  • 23
  • 43
2

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);
}
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • @BrianThompson Hi! To be honest, I didn't really look at your answer. Since the question was clear and the mistake obvious. So we seem to have similar answers. And because the same code is really unnecessary, I'll delete my code but leave my answer text. okay? I really didn't get it! – Maik Lowrey Dec 13 '21 at 16:23
  • @BrianThompson I have the fresh() method inside. I putted to the response json. take a look ` 'data'=> $seller->fresh()], 200)` – Maik Lowrey Dec 13 '21 at 16:24
  • 1
    There's no need to remove the code from your answer. If your answer was honest then I'll take your word for it and remove my comment. It was just suspicious to me that your code snippet added a space in the same place I did, but made no other formatting changes. – Brian Thompson Dec 13 '21 at 16:25
  • 1
    I did not see the `fresh()` call either since the snippet scrolls horizontally (at least on my screen). Which actually pointed out that I had my methods mixed up and meant to use `refresh` instead of `fresh`. You may want to point out the usage of `fresh` in the text of your answer so that future readers don't miss it like I did. – Brian Thompson Dec 13 '21 at 16:33
  • @BrainThompson I updated. Thanks for advice! ;-) – Maik Lowrey Dec 13 '21 at 16:39
0

In my case $user = tap(User::where('id', $id))->update(['name' => $name]);. Now, you can return user: return $user->first();