I have a MenuItem
model which uses menu_items
table with the following fields:
$table->bigIncrements('id');
$table->string('name')->nullable(false);
$table->unsignedBigInteger( 'category_id')->nullable(false);
$table->text('description')->nullable();
$table->string('image')->nullable();
$table->decimal('price')->nullable(false);
$table->boolean('available')->default(true);
$table->boolean('active')->default(true);
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories');
Now, there are certain menu items which can have sides. These sides are also menu items and are defined by its category (side
category).
For instance: I wanna have a MenuItem
that belongs to a MainDish
category. For this special category a menu item can have sides. These sides are also MenuItem
. So I want to store the sides associated to a MainDish
.
To do so, I've created a menu_item_sides
migration:
Schema::create('menu_item_sides', function (Blueprint $table) {
$table->primary(['main_item_id', 'side_item_id']);
$table->unsignedBigInteger('main_item_id');
$table->unsignedBigInteger('side_item_id');
$table->timestamps();
$table->foreign('main_item_id')
->references('id')
->on('menu_items')
->onDelete('cascade');
$table->foreign('side_item_id')
->references('id')
->on('menu_items')
->onDelete('cascade');
});
and its respective MenuItemSide
model. As you can see, menu_item_sides
has a composite key with
main_item_id', 'side_item_id'
and both keys point to the same PK in menu_items
.
In this specific case, how do I declare the relationships between the MenuItem
and MenuItemSide
models?