Laravel 附加字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26372688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:16:30  来源:igfitidea点击:

Laravel attach with extra field

laravelpivot

提问by dulan

I have 3 tables, products, imagesand product_image. The 3rd one is a pivoting table. Other than product_idand image_idin product_imagetable, I also have 2 extra fields in this pivoting table: positionand type, now for an existing productmodel A, how do I attach an image to A and at the same time set up its positionand typevalue in that pivoting record? Thanks.

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

回答by Razor

You may try something like this:

你可以尝试这样的事情:

$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']]);