samedi 25 juin 2016

Many to Many to Many relation in laravel

Here my tables migrations(4):

restaurants:

Schema::create('restaurants', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
});

foods:

Schema::create('foods', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
});

ingredients:

Schema::create('ingredients', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
});

restaurant_has_foods_with_ingredients:

Schema::create('restaurant_has_foods_with_ingredients', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('restaurant_id');
        $table->unsignedInteger('food_id');
        $table->unsignedInteger('ingredient_id');

        $table->foreign('restaurant_id')
            ->references('id')
            ->on('restaurants')
            ->onDelete('cascade');

        $table->foreign('food_id')
            ->references('id')
            ->on('foods')
            ->onDelete('cascade');

        $table->foreign('ingredient_id')
            ->references('id')
            ->on('ingredients')
            ->onDelete('cascade');
});

How can I define my Restaurant,Food,Ingredient Models with their relations?

Here some examples of my needs:

1-All restaurants with specific ingredient in their serving dishes.

2-All ingredients of a specific dish in a specific restaurant.

3-All dishes with a specific ingredient in a restaurant.

...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire