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

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.
}
Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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

		
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.