我有发贴表和图片表有一对多关系。Laravel插入一对多关系
邮型号代码
public function images()
{
return $this->hasMany('App\PostImage');
}
我从用户3倍的图像,我想将它们插入与文章ID图像表。
例如
firstImage.jpg与交ID
secondImage.jpg与交ID
thirdImage.jpg与PO ST ID
问题是
如何插入在同一时间与ID为1的三个图像在分三路
我已经试过
我做了一个for循环插入3次表,但我知道这不是一个好的做法,这里是代码。
for($x = 0; $x < 3; $x++) {
$image = new PostImage;
$image -> post_id = $ad -> id;
$image -> image = $images_name[$x];
$image -> save();
}
重复的:HTTP:// stackoverflow.com/questions/29723865/how-to-insert-multiple-rows-from-a-single-query-using-eloquent-fluent –
[如何使用eloquent/fluent从单个查询中插入多行] (http://stackoverflow.com/questions/29723865/how-to-insert-multiple-rows-from-a-single-query-using-eloquent-fluent) –