How to add many to many extra pivot columns?

Viewed 153

I have two tables products and orders which are linked by a pivot table order_product with the id of each table. But I also want to add an amout column to the pivot table. How can I do this with laravel Voyager?

1 Answers

Easily just define it in your migration file of order_product and access it from relation function as bellow:

Schema::create('order_product', function (Blueprint $table) {
    $table->unsignedBigInteger('order_id');
    $table->unsignedBigInteger('product_id');
    $table->unsignedInteger('amount');
});

and for example in your product model:

public function orders()
{
    return $this->belongsToMany(Order::class)
        ->withPivot('amount');
}
Related