Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

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.

0

@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

Last updated 9 years ago.
0

@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?

0

I have no idea what you're talking about :) Show a piece of code you mean.

0

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);

?

0
$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().

0

@Fritzberg

== 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:

  1. you need $user->userPhones->add($phone) in order to add something to the collection

  2. there is no add method on the relation (which is 1userPhones()1 )

  3. 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.

Last updated 9 years ago.
0

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.

Last updated 9 years ago.
0

$phone->id_user is not default foreign key, so you should specify it in the user() relation as 2nd param.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

hieroshima hieroshima Joined 16 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.