eloquent save () method does not work

0

When I try to use the save() method of eloquent-laravel 5.1, in the way it asks for documentation, the update is not done in the database.

This is how the documentation asks for it:

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

This is mine:

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 data is being pulled normally from the database after find() , even the 'active' attribute is equal to 1. Within if , dd($animal->ativo) returns 0, ie up there the code is ok. The dd($animal->save()) also returns true and is normally redirected to the index, however, the update does not appear in db.

Any tips? Save me who can. Thankful.

    
asked by anonymous 07.02.2017 / 00:40

2 answers

0

In view, when you marked the animal as "adopted" , a modal was displayed asking for confirmation. After removing the modal from the view, save () worked normally. The question I got was: Why?

The 'confirm' of the modal displayed the path 'animal / adopted / 6' where 6 is the id of the animal. Exactly the same way done without modality. In action, I confirmed that I received $ id in both ways, with or without modal. The difference is that one way did not change and the other did. I am a beginner and I do not know why this happened, and maybe it is not even an adequate answer, but taking the modal from the view solved.

    
07.02.2017 / 21:58
0

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

Some steps to solve:

  • Use try / catch on your methods, and put a dump () on the exception.
  • All errors are stored in the laravel.log file, which is located in the directory storage / logs / laravel.log. You can check the because it is not saving.
  • 08.02.2017 / 00:50