samedi 13 octobre 2018

Update multiple savemany relations

I have a User table with 2 associated tables (Contacts en Addresslines).

The User can have more than 1 Contact and more than 1 AddressLine (savemany).

When i try to edit a user and update the user record, i find the user:

$user = User::find($id);

Now i have the $user object. But the problem i have is that both Addressline and Contact are an array inside $user. At this point i update the record by using this:

    $user->addressLines[0]->postalcode = $request->postalcode;
    $user->addressLines[0]->housenumber = $request->housenumber;
    $user->addressLines[0]->housenumbersuffix = $request->housenumbersuffix;
    $user->addressLines[0]->street = $request->street;
    $user->addressLines[0]->city = $request->city;

    $user->contacts[0]->phonenumber = $request->phonenumber;
    $user->push();

This works, but i really want to save the savemany records without using a [0].

Can i still use the $user object for that, or is there a better way?

If i have to give more information plz let me know.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire