Edit record in database

Asked

Viewed 959 times

0

I’m trying to edit a form using fill()->save() but instead of updating Laravel 5 tries to do insert and gives error saying that the record already exists in the table.

$input = $request->all();
$record->fill($input)->save();

I’m using rules: class MeuFormRequest extends Request

'nome' => 'required|unique:empresa,nome' . ($id ? ",$id" : '') 

Does anyone have any idea what might be wrong?

  • Give first a find to search the data with those values, then you play the parameters as the new value and after a save. Ex: $user = User::find(1); $user->email = '[email protected]'; $user->save();. Or you can do direct User::Where('votes', '>', 100)->update(['status' => 2]);

  • That’s what it was. Thanks!

1 answer

3

Put a larger chunk of code. So that the Eloquent recognize as update, you need to give a GET of the object before, otherwise it will be considered as a new record, because it has no ID related,

Ex:

$model = Model::find(1);
$model->fill($input);
$model->save();
  • That’s what it was. Thanks!

Browser other questions tagged

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