You most likely can do something like:
$user->setRelation('userPhones', [$userPhone]);
$user->push();
I may be wrong as I haven't done it before, but I've read the source code for the stuff you want to do and it seems a proper way to do that.
@zaalbarxx
It won't associate the related model, so push
will save that model without foreign key set, thus the same error will occur.
@hieroshima Read this: http://stackoverflow.com/a/24915078/784588
@jarektkaczyk: Thank you, but if i do save() on my main object it isn't the same as ::find($id)? I mean it isn't already exists?
I have no idea what you're talking about :) Show a piece of code you mean.
Look at my code from first post. I'm creating new user then i'm doing save() on user object. From now i have this user in DB with primary key (foreign key for related object) then i'm adding related model into main object (user). In stackoverflow that you sent i read "push would do the job, but only if you do it with already existing parent AND you first associate child models" so my question: When i do save() method on created object it isn't existing parent after save? Other way:
Is that:
$user = new User();
$user->name = "Test";
$user->save(); //i have main object in my DB PK = 1
equal to:
$user = User::find(1);
?
$once = User::find(1);
$again = User::find(1);
dd($once == $again);
I didn't run it but I think it would say false. That are two objects representing the same, but that are TWO separate objects.
Check the documentation how to save your relationship. You should probably use associate().
==
will return true, ===
would return false
http://php.net/manual/en/language.operators.comparison.php
@hieroshima Yes, the parent is OK at that moment, but you're doing something, that couldn't work:
$user->userPhones()->add($phone)
This is totally wrong:
you need $user->userPhones->add($phone)
in order to add something to the collection
there is no add
method on the relation (which is 1userPhones()1 )
even if you add it to the collection like above, it woulnd't associate the child with the parent - see the SO link
So you need eg.:
$user->userPhones()->save($phone);
// or
$phone->user()->associate($user);
$user->userPhones->add($phone);
// or
$phone->user_id = $user->id;
$user->userPhones->add($phone);
so obviously the 1st method shown is the best.
Hi, Ok now i know that associate() i can only use on belongsTo objects. But if i'm doing that:
$phone->user()->associate($user);
$user->userPhones->add($phone);
and when i'm doing push on $user i got infinity loop - "Maximum function nesting level of '100' reached, aborting!" i know that this is error from xDebug but it is mean that something wrong :).
I checked $user in xDebug and this is realy infinity loop - i got this:
$user obj have relations $phone has $user has $phone has $user ... n
Edit:
If i do:
$phone->id_user = $user->id;
$user->userPhones->add($phone);
It is ok - don't have infinity loop.
$phone->id_user
is not default foreign key, so you should specify it in the user()
relation as 2nd param.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community