Save Related Models (One to One) - Laravel 5.1

Asked

Viewed 94 times

0

I have a Model Info and a Model Complement that are related one to one.

The relationships are already working properly, I can pull the data and such, but I need to do an update and do not know how.

Currently I receive all data on request, but I just do the update table Infos, as I would also update the table data complements in accordance with the info to which it belongs?

2 answers

1


I was able to resolve this issue as follows:

$complemento = Complemento::firstOrNew(['id_info' => $id]);

//requests...

$complemento->save();  

was even better since I had the need to also create the complement if it did not exist.

0

I’m pretty sure you can do it like this:

 $info = Info::findOrFail($id);

 $info->complemento->fill($request->all())->save();
  • Hello @Wallacemaxters, I even tried to do it that way now, but I couldn’t. Anyway I ended up finding a better solution for my need with firstOrNew

Browser other questions tagged

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