Один-ко-Многим

Миграции для моделей
create-brands-table
public function up()
{
    Schema::create('brands', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->text('description');
        $table->timestamps();
    });
}
create-products-table
public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('brand_id');
        $table->foreign('brand_id')
          ->references('id')->on('brands')
          ->onDelete('cascade');
        $table->string('name');
        $table->string('slug');
        $table->double('price');
        $table->integer('qty');
        $table->text('description');
        $table->timestamps();
    });
}

Пример отношенияОдин-ко-Многим Brand и Product

  • Product может принадлежать только одному Brand

  • Brand может иметь много Product

Получение данных «Один-ко-Многим»

Создание отношения «Один-ко-Многим»

Удаление отношения «Один-ко-Многим»

Last updated