yes you can do it with eloquent, here is a simple example:
$post = new Post();
$post->title='nice title';
$post->save(); //saving the post will set the id on this post model.
for($i = 1; $i <= $numberOfImages; $i++)
{
$image = new Image();
$image->path = 'path you want to set';
$post->images()->save($image); // will save the image for the post.
}
Is this also performance wise OK? Its doin a lot of queries right now? I mean, on insert with multiple records is better than a query for each record, right?
Alternatively, to reduce queries you can insert multiple records like :
Image::insert([
[
'path'=>'some path',
'post_id'=>$post->id
],
[
'path'=>'another path',
'post_id'=>$post->id
]
...
...
]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community