【问题标题】:How to add many to many extra pivot columns?如何添加多对多的额外数据透视列?
【发布时间】:2022-01-16 04:05:45
【问题描述】:

我有两个表 products 和 orders,它们由一个带有每个表的 id 的数据透视表 order_product 链接。但我也想在数据透视表中添加一个 amout 列。我如何使用 laravel Voyager 做到这一点?

【问题讨论】:

    标签: laravel database voyager


    【解决方案1】:

    只需在 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');
    }
    

    【讨论】:

    • 我的意思是在 Laravel Voyager 里面
    猜你喜欢
    • 2021-07-12
    • 2018-06-27
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    相关资源
    最近更新 更多