我有 3 个表、产品、图像和 product_image。第三个是旋转台。除了 product_image 表中的 product_id 和 image_id 之外,我在这个数据透视表中还有 2 个额外的字段:位置和类型,现在对于现有的产品模型 A,我如何将图像附加到 A 并同时设置它的位置和在该旋转记录中键入值?谢谢。
最佳答案
你可以尝试这样的事情:
$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']]);
关于Laravel 附加额外字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26372688/