How to return an updated data after updating in Laravel Model?
Asked Answered
C

5

5

I have a model Driver which have columns: name, branch, status_id, etc..Updating is actually fine and working, my problem is how can I return the updated one?

Here's what I tried so far, but it returns a boolean, resulting of returning an error in my console:

The Response content must be a string or object implementing __toString(), "boolean" given.

public function updateStatus(Driver $driver)
{
    return $driver->update($this->validateStatus());
}

public function validateStatus()
{
    return $this->validate(request(), [
        'status_id' => 'required|min:1|max:3'
    ]);
}

I expect it should return the all the columns of a driver.

I've been to this link but it doesn't helped. Someone knows how to do this?

Continual answered 7/2, 2020 at 12:23 Comment(1)
hey can you please check my answer i have solution without model helper direct in controller you can returnKomarek
A
4

return as object instead of boolean type

public function updateStatus(Driver $driver)
{
   $driver->update($this->validateStatus());
   return $driver;// first way
   // return tap($driver)->update($this->validateStatus()); //second way
}

public function validateStatus()
{
    return $this->validate(request(), [
        'status_id' => 'required|min:1|max:3'
    ]);
}
Animality answered 7/2, 2020 at 12:28 Comment(1)
I wish there could be a one line solution :) But yes, this will work. ThanksContinual
K
13

You can use tap() helper, which will return updated object after the update like so:

return tap($driver)->update($this->validateStatus());

More on that here: Tap helper

Kalakalaazar answered 7/2, 2020 at 12:39 Comment(0)
A
4

return as object instead of boolean type

public function updateStatus(Driver $driver)
{
   $driver->update($this->validateStatus());
   return $driver;// first way
   // return tap($driver)->update($this->validateStatus()); //second way
}

public function validateStatus()
{
    return $this->validate(request(), [
        'status_id' => 'required|min:1|max:3'
    ]);
}
Animality answered 7/2, 2020 at 12:28 Comment(1)
I wish there could be a one line solution :) But yes, this will work. ThanksContinual
R
3

It's work for me

   $notify = tap(Model::where('id',$params['id'])->select($fields))->update([
            'status' => 1
        ])->first();
Rostand answered 27/8, 2021 at 3:28 Comment(0)
K
2

I think no need any model helper for that

in controller you can do like this

$driver = Driver::find(1);
$driver->name = "expmale";
$driver->save();

return $driver;

or other way

$driver = Driver::find(1);
$driver->update([
      'name'=> "expmale"
      ]);

return $driver;
Komarek answered 7/2, 2020 at 12:48 Comment(1)
This strategy won't return the updated name this will only return the data before update as you use find method before update.Calx
D
0

I know that there's already an answer for this, but ideally, you don't want to use the update method. It's just a model helper method that doesn't really add much. Internally it does what I have included below, except it returns the result of save().

You'd want to do something like this:

if ($driver->fill($this->validateStatus)->save()) {
    return $driver;
}

throw new \RuntimeException('Update failed, perhaps put something else here);

The problem you're going to have with the accepted answer (and most of the others) is that you return the model without ever checking that it was actually updated, so you're going to run into issues down the line when it's not updating the actual database even though it's reporting that it is.

Derinna answered 7/2, 2020 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.