4

I am struggling to use laravel push() function

My Code is here:

$company = new Company(array('name' => 'Company 1'));
$company->persons[] = new Person(array('name' => 'Company 1 Person 1', 'mobile' => 12345));
$company->persons[] = new Person(array('name' => 'Company 1 Person 2', 'mobile' => 112233));
$company->push();

When it gets stored: Company gets stored well, but for company persons, it stores company_id to 0

Larave Documentations says:

Sometimes you may wish to save not only a model, but also all of its relationships. To do so, you may use the push method:

$user->push();

what is wrong?

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62
  • I've never used `push()` before but it seems like maybe you aren't using it correctly. The docs don't give much mention to it. See [this question](http://stackoverflow.com/questions/17035682/eloquent-push-and-save-difference) – Jared Eitnier Jul 23 '14 at 14:57
  • I have a feeling that push will only work with pre-defined relationships, not creating new records. – Jono20201 Jul 23 '14 at 15:01

1 Answers1

4

It's not Doctrine ;)

push could do it, BUT it wouldn't be nice and easy (below you can find out how)

And here's what you can do instead (nice and easy way):

// still you need already existing Company, so save it first
$company = new Company(array('name' => 'Company 1'));
$company->save();

$company->persons()->createMany(array(
  array('name' => 'Company 1 Person 1', 'mobile' => 12345),
  array('name' => 'Company 1 Person 2', 'mobile' => 12345)
));

This will save new Person models and associate them with the Company


push would do the job, but only if you do it with already existing parent AND you first associate child models:

$company = Company::first(); // or any existing

$person = new Person(array('name' => 'Company 1 Person 1', 'mobile' => 12345));
$person->company()->associate($company);

$anotherPerson = new Person(array('name' => 'Company 1 Person 2', 'mobile' => 12345));
$anotherPerson->company()->associate($company);

$company->persons[] = $person;
$company->persons[] = $anotherPerson
$company->push();

Obviously this is more work and it's definitely not the way I would suggest.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • 1
    @decoz, then don't you agree that the line written in doc is very confusing? They say "you may wish to save not only a model, but also all of its relationships"??? – Rajan Rawal Jul 23 '14 at 16:45
  • I'm not going to argue with that. However, to be precise, Eloquent is saving those models, only it doesn't associate them with the relation parent, which is obviously unexpected. Like I said, it's not Doctrine. It handles relations differently, sometimes you'd expect more from it, but that's how it works currently. – Jarek Tkaczyk Jul 23 '14 at 16:55
  • ha ha ah!!! sure second method is not at all preferred. In short `push()` can be used only with existing one. I didn't know `createMany` method. Thanks! – Rajan Rawal Jul 23 '14 at 16:56
  • @JarekTkaczyk very usefull your answer. I have a question, if I call in your first example $company->persons it will be lazy loaded?, I mean will query the DB or it is in memory? – Armando Rodríguez Acosta May 04 '20 at 23:24