Eloquent save() method does not work

Asked

Viewed 451 times

3

When I try to use the method save() eloquent-Variable 5.1, in the manner requesting the documentation, the update is not made in the bank.

This is how you ask for documentation:

$flight = App\Flight::find(1);    
$flight->name = 'New Flight Name';    
$flight->save();

This is my:

public function adotado($id) {         
        $animal = Animal::find($id); 
        if (Auth::user()->id == $animal->user_id) {
            $animal->ativo = 0;
            $animal->save();
            return redirect()->action('UserController@index');
        } else {
            return view('errors.restrito');
        }
    }

I checked with dd($animal) that the data is normally being pulled from the database after the find(), including the active attribute is 1. Inside the if, dd($animal->ativo) returns 0, that is, until then the code is ok. The dd($animal->save()) also returns true and is redirected normally to index, however the update does not appear in db.

Any tips? Save me as many as you can. Thank you.

  • Try commenting on redirect and check if there are any errors

  • No mistake. =\

  • Checks the return of save, may be returning false. Would be the method push useful in that case?

  • @Virgilionovic the change is being made in the active $animal->that is 1 in db. After I set active = 0 within the if, return of dd($animal->active) was 0, then had change.

  • @Andersoncarloswoss the return of save() is 1. I tried to push as well and continued in it.

2 answers

1

In view, when marking the animal as "adopted", one modal was displayed asking for confirmation. After removing the modal from the view, save() worked normally. The question I’m left with is: why?

'confirm' of modal displayed path 'animal/adopted/6' where 6 is the animal id. Exactly the same way done without modal. In action, I confirmed that I received $id in both ways, with or without modal. The difference is that one way it didn’t change and the other way it did. I’m a beginner and I don’t know why this happened, and maybe it’s not even a proper response, but taking the modal of the view solved.

1

Do not use redirect()->action() friend. Create routes for your methods, it will be much simpler.

A few steps to solve:

  1. Use Try/catch in your methods, and dump() Exception.
  2. All errors are stored in the.log file, which is in the directory Storage/logs/Laravel.log. You can check there the because you’re not saving.
  • Thanks for the tip. I will use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.