Support the ongoing development of Laravel.io →
Input Database Eloquent
Last updated 1 year ago.
0

Take a look at the docs: http://laravel.com/docs/4.2/eloquent#inserting-related-models

Laravel will take care of you and insert the ids as required.

$comments = array(
    new Comment(array('message' => 'A new comment.')),
    new Comment(array('message' => 'Another comment.')),
    new Comment(array('message' => 'The latest comment.'))
);

$post = Post::find(1);

$post->comments()->saveMany($comments);

Hopefully that will give you some idea of what you need to do. Let me know if it doesn't!

Last updated 1 year ago.
0

Hi,

Thanks for your answer...

ok... that means i have to save the new child reference through the relation in the parent reference... i am sure this will work, but also means that i have to load the parent object first ($post i the example).

Mhh, in my case i would not save many childs (comments) at once but only one single child reference i create by a form.

My idea was to avoid loading the whole parent object ($post) because the only thing i need is the id of the parent that is provided by the form data. I hoped, the automatic binding of the form data to the model attributes / table fields will do this for me even if it is defined as reference.

Thanks Sascha

Last updated 1 year ago.
0

ok... just setting the property is the solution...

i did use Input::all() for test purposes and this did not work.

After having some annoying tries with relations, I think i really didn't understand them well, when to use attach, detach, save, associate, and so on why sometimes functions are useable by using the relation object and why sometimes not.

So why does this work (store):

public function store() {
...
 $data = Input::all();
 $reference = Reference::create($data);
 $newParent = Reference::find($data['parent_reference']);
                if ($newParent != null) {
                    $parent = Reference::find($reference->parent_reference);
                    $parent->childReferences()->detach($reference->parent_reference);
                    $newParent->childReferences()->save($reference);
                }
...
}

And why does this not work (update):

public function update($id){
...
 $data = Input::all();
 $reference = Reference::find($id);
 $newParent = Reference::find($data['parent_reference']);
                if ($newParent != null) {
                    $parent = Reference::find($reference->parent_reference);
                    $reference->parentReference()->childReferences()->detach($reference->parent_reference);
                    $newParent->childReferences()->save($reference);
                }
...
}

i Also tried to replace

$parent->childReferences()->detach($reference->parent_reference);

by

 $parent = Reference::find($reference->parent_reference);
 $parent->childReferences()->detach($reference->parent_reference);

also tried to set

$reference->childReferences = null;

and

$reference = Reference::with('parentReference')->find($id);

but i always got different exceptions that the methods cannot be found when trying to access them through the relations.

Is there anywhere a good tutorial? The doc didn't really helped me to understand it.

Thank you

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sclaren sclaren Joined 2 Jul 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.