我有三张桌子,products、images和product_image.第三个是数据透视表.除了product_image表中的product_id和image_id之外,我在这个数据透视表中还有两个额外的字段:position和type,现在对于现有的product型号A,我如何将图像附加到A,同时在数据透视记录中设置其position和type值?谢谢
我有三张桌子,products、images和product_image.第三个是数据透视表.除了product_image表中的product_id和image_id之外,我在这个数据透视表中还有两个额外的字段:position和type,现在对于现有的product型号A,我如何将图像附加到A,同时在数据透视记录中设置其position和type值?谢谢
你可以试试这样:
$product = Product::find(1);
// if you need also to save the image
$image = new Image(array('foo' => 'bar'));
$product->images()->save($image,array('position'=>'foo', 'type'=>'bar' ));
// if you know image id
$product->images()->attach([$imageId => ['position'=>'foo', 'type'=>'bar']]);