Laravel 5.1 eloquent tries to INSERT into DB instead of UPDATE Laravel 5.1 eloquent tries to INSERT into DB instead of UPDATE laravel laravel

Laravel 5.1 eloquent tries to INSERT into DB instead of UPDATE


You need to set a variable to the found item

$m = $m->whereField($field)->first();if ($m->exists()) {$m->anotherField = $value;$m->save();}

As you have it now, $m is only refering to new Model(), not to the found item.


You could also do:

$m = $m->whereField($field)->firstOrFail();

//catch exception... (log or something else)

$m->anotherField = $field;$m->save();

Doing this will let you catch the exception so you can log and display an error page as necessary. To catch the ModelNotFoundException, add some logic to your app/Exceptions/Handler.php file.

http://laravel.com/docs/5.0/eloquent