Question : add field to many to many relationship laravel
Answered by : snippets
//in Model
public function products()
{ return $this->belongsToMany(Product::class) ->withPivot('status') ->withTimestamps();
}
//in Migration
Schema::create('vendor_product', function (Blueprint $table) { $table->unsignedBigInteger('product_id'); $table->foreign('product_id', 'product_id_fk_6766249')->references('id')->on('products')->onDelete('cascade'); $table->unsignedBigInteger('vendor_id'); $table->foreign('vendor_id', 'vendor_id_fk_6766249')->references('id')->on('vendors')->onDelete('cascade'); $table->string('status'); $table->timestamps();
});
Source : | Last Update : Fri, 24 Jun 22