Laravel 附加额外字段

标签 laravel pivot

我有 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/

相关文章:

php - 如何在 Visual Studio Code 中使用 HTML 标记格式化 PHP 文件?

php - 解析 YML 时将变量从 PHP 传递到 YML 文件

sql - 查询以水平显示输出

sql-server - MS-SQL 透视两列

python - 将具有不同名称的多列上的数据帧从宽格式转换为长格式

php - SQL 数据库中用户名的默认值

php - laravel TokenMismatchException 文件上传超过 8MB

Laravel 5.1 更新数据透视表列而不删除现有记录

mysql - 在多个连接表上使用 MySQL GROUP_CONCAT 或 PIVOT

excel - Excel 中的数据整理 - 重新排列列和行