Многие-ко-Многим

Миграции для моделей
create-categories-table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('description')->nullable();
$table->timestamps();
});
}create-products-table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->float('price');
$table->text('description')->nullable();
$table->timestamps();
});
}create-category_product-table
public function up()
{
Schema::create('category_product', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('product_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('product_id')->references('id')->on('products');
});
}Пример отношения Многие-ко-Многим Category и Product
Product может принадлежать многим Category
Category может принадлежать многим Product
Получение данных «Многие-ко-Многим»
Создание отношения «Многие-ко-Многим»
Удаление отношения «Многие-ко-Многим»
Last updated