php – eloquent's save() method does not work

Question:

When I try to use the save() method of eloquent-laravel 5.1, as requested by the documentation, the update is not done in the database.

This is how the documentation asks for:

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

Any tips? Save me whoever you can. Grateful.

Answer:

In view, when marking the animal as "adopted" , a modal was displayed asking for confirmation. After removing the modal from the view, save() worked normally. The question that remained for me is: 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 modal. In action, I confirmed that I got the $id 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 an adequate answer, but taking out the modal of the view it solved.

Scroll to Top