Php laravel中的多对多关系

Php laravel中的多对多关系,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,这里是我的表迁移(4): 餐厅: Schema::create('restaurants', function (Blueprint $table) { $table->increments('id'); $table->string('name'); }); 食物: Schema::create('foods', function (Blueprint $table) { $table->increments('id');

这里是我的表迁移(4):

餐厅:

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

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

Schema::create('ingredients', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
});
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');
});
餐厅有配料的食物:

Schema::create('ingredients', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
});
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');
});
我如何定义我的餐厅、食物、配料模型及其关系

以下是我需要的一些例子:

1-所有在其服务菜肴中含有特定成分的餐厅

2-特定餐厅特定菜肴的所有成分

3-餐厅中含有特定成分的所有菜肴

-------------------------编辑后-----------------------------

我有自己的解决办法,但我认为这不是一个好办法

现在,在我的餐厅模式中,我有两种获取食物的方法

要获得餐厅的所有食物,请执行以下操作:

public function foods()
{
    return $this->belongsToMany('App\Models\Food', 'restaurant_has_foods_with_ingredients')
        ->groupBy('food_id');
}
另一个是获取当前餐厅特定食物的成分

public function foodIngredients(Food $food)
{
    $result = DB::table('restaurant_has_foods_with_ingredients')
        ->select('restaurant_has_foods_with_ingredients.ingredient_id as ingredient_id')
        ->where('restaurant_has_foods_with_ingredients.restaurant_id',$this->id)
        ->where('restaurant_has_foods_with_ingredients.food_id',$food->id)
        ->get();

    $ingredients = array();

    foreach ($result as $row) {
        $ingredients[] = Ingredient::find($row->ingredient_id);
    }
    return $ingredients;
}

基本上是这样的:

创建两个迁移:
restaurant\u food
food\u配料

我们有一个

餐厅模型-食品模型-配料模型

一家餐馆可以有多种食物,一种食物可以在餐馆里供应->所以我们这里有一个多对多的关系

餐厅模式

class Restaurant extends Model
{
    /**
     * The foods that belong to the Restaurant.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
class Food extends Model
{
     /**
     * The ingredients that belong to the Food.
     */
    public function restaurants()
    {
        return $this->belongsToMany('App\Restaurant');
    }
    /**
     * The ingredients that belong to the Food.
     */
    public function ingredients()
    {
        return $this->belongsToMany('App\Ingredient');
    }

}
class Ingredient extends Model
{
    /**
     * The foods that belong to the Ingredient.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
好了,接下来的事情

1-正如我们前面提到的,一种食物类型可以在许多餐厅提供,因此我们需要定义反向关系

2-一种食物有多种成分,一种成分可以用于多种食物->另一种多对多

食品模型

class Restaurant extends Model
{
    /**
     * The foods that belong to the Restaurant.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
class Food extends Model
{
     /**
     * The ingredients that belong to the Food.
     */
    public function restaurants()
    {
        return $this->belongsToMany('App\Restaurant');
    }
    /**
     * The ingredients that belong to the Food.
     */
    public function ingredients()
    {
        return $this->belongsToMany('App\Ingredient');
    }

}
class Ingredient extends Model
{
    /**
     * The foods that belong to the Ingredient.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
现在同样的情况也发生在

成分模型

class Restaurant extends Model
{
    /**
     * The foods that belong to the Restaurant.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
class Food extends Model
{
     /**
     * The ingredients that belong to the Food.
     */
    public function restaurants()
    {
        return $this->belongsToMany('App\Restaurant');
    }
    /**
     * The ingredients that belong to the Food.
     */
    public function ingredients()
    {
        return $this->belongsToMany('App\Ingredient');
    }

}
class Ingredient extends Model
{
    /**
     * The foods that belong to the Ingredient.
     */
    public function foods()
    {
        return $this->belongsToMany('App\Food');
    }

}
好了,现在一切都准备好了这就是它的用法

增加关系

$Restaurant = Restaurant::find($id);
$Restaurant->foods()->attach($food_id);
$Restaurant->foods()->detach($food_id);
解除关系

$Restaurant = Restaurant::find($id);
$Restaurant->foods()->attach($food_id);
$Restaurant->foods()->detach($food_id);
1-所有在其服务菜肴中含有特定成分的餐厅

2-特定餐厅特定菜肴的所有成分

3-餐厅中含有特定成分的所有菜肴

用一个变量改变马铃薯/纽约,你就可以出发了


我的代码可能有一些小的输入错误或错误,但我希望你能了解事情的运作方式

谢谢你的回答,但问题是:两个餐厅的同一道菜有不同的成分(不同餐厅的同一道菜的成分可能不同)。首先,你应该提到,我会尝试和编辑你能给我一个预期结果的例子吗?这样我就可以努力工作,并尝试如何实现它?例如,纽约的一家餐厅提供带番茄的馅饼,但洛杉矶的另一家餐厅提供不带番茄的馅饼,所以我需要餐厅有带配料的食物,正如我在问题中提到的,找到什么配料每家餐厅都用馅饼皮做馅饼。我有自己的解决方案,我现在编辑并把它放在我的问题中,但我认为这不是一个好的解决方案。