Laravel附加额外的领域

dul*_*lan 12 pivot laravel

我有3个表,产品,图像product_image.第三个是旋转表.除了PRODUCT_IDimage_idproduct_image表,我也有在该转动表2个额外的字段:位置类型,现在现有的产品型号A,我怎么把图像附A和设立,同时位置和该转轴记录中的类型值?谢谢.

Raz*_*zor 23

你可以尝试这样的事情:

$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']]);
Run Code Online (Sandbox Code Playgroud)

  • 以下工作在laravel 5 $ table1-> relation() - > attach($ attributes ['array_field'],["field"=> $ attributes ["field"]]); (3认同)