I have Post table and images table that have one to many relationship.
Post Model Code
public function images()
{
return $this->hasMany('App\PostImage');
}
I take from the user 3 images and I want to insert them in the images table with the post id.
For example
firstImage.jpg with post id 1
secondImage.jpg with post id 1
thirdImage.jpg with post id 1
Question is
How I insert the three images at the same time with id 1 in three columns
What I have tried
I have made a for loop that insert 3 times in the table but I know it is not a good practice and here is the code.
for($x = 0; $x < 3; $x++) {
$image = new PostImage;
$image -> post_id = $ad -> id;
$image -> image = $images_name[$x];
$image -> save();
}