简体   繁体   中英

Laravel Create Model for a pivot table

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?

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:

  #Example for MenuItem model
public function menuItemSides()
{
    return $this->belongsToMany('App\MenuItemSide', 'menu_item_sides', 'main_item_id', 'side_item_id');
}

#Example for MenuItemSide model
public function menuItems()
{
    return $this->belongsToMany('App\MenuItem', 'menu_item_sides', 'side_item_id', 'main_item_id');
}

Or See: https://laravel.com/docs/6.x/eloquent-relationships#many-to-many

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM