【发布时间】:2022-01-16 04:05:45
【问题描述】:
我有两个表 products 和 orders,它们由一个带有每个表的 id 的数据透视表 order_product 链接。但我也想在数据透视表中添加一个 amout 列。我如何使用 laravel Voyager 做到这一点?
【问题讨论】:
我有两个表 products 和 orders,它们由一个带有每个表的 id 的数据透视表 order_product 链接。但我也想在数据透视表中添加一个 amout 列。我如何使用 laravel Voyager 做到这一点?
【问题讨论】:
只需在 order_product 的迁移文件中定义它,然后从关系函数中访问它,如下所示:
Schema::create('order_product', function (Blueprint $table) {
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->unsignedInteger('amount');
});
例如在您的产品模型中:
public function orders()
{
return $this->belongsToMany(Order::class)
->withPivot('amount');
}
【讨论】: