【问题标题】:Laravel - Check if in many to many relationshipLaravel - 检查是否存在多对多关系
【发布时间】:2014-05-28 16:48:14
【问题描述】:

用户可以将食材保存到购物清单中。现在,当用户登录并访问食谱时,购物清单小部件会向他显示他的清单上已经有哪些成分,这就是我的问题。

我在这里获取食谱的所有成分:

$recipe = Recipe::find($id);
$recipe->load('ingredients');  

在 $recipe->ingredients 上使用 foreach 工作正常

在这里,我正在获取用户对这个食谱的购物清单,如果他有的话:

if(Auth::check()) {
  $list = ShoppingList::with('ingredients')->where('recipe_id',$id)->where('user_id',Auth::user()->id)->get();   
//... 
}  

这里我想检查一下,如果一个保存的成分在购物清单上:

            foreach($recipe->ingredients as $i) {
            foreach($list as $l) {
                $ingredient = Ingredients::where('id','=',$l->ingredients_id)->get();
                $i->isAdded = (count($ingredient) > 0) ? 1 : 0;
            }

        }  

但不知何故,这是完全错误的。我错过了什么?

关系:
配方:

    public function user() {
        return $this->belongsTo('User','user_id');
}
public function lists() {
        return $this->hasMany('ShoppingList','recipe_id');
}

public function ingredients() {
        return $this->belongsToMany('Ingredients','ingredients_recipe','recipe_id','ingredients_id')->withPivot('amount');
}  

购物清单:

    public function user() {
        return $this->belongsTo('User','id');
}
public function recipe() {
        return $this->belongsTo('Recipe','recipe_id');
}
public function ingredients() {
        return $this->belongsToMany('Ingredients','shopping_list_ingredients','shopping_list_id','ingredients_id')
                    ->withPivot(array('unit','amount'))
                    ->withTimestamps();
}  

成分:

    public function recipes() {
        return $this->belongsToMany('Recipe','ingredients_recipe','recipe_id','ingredients_id')->withPivot('amount');
}
public function lists() {
        return $this->belongsToMany('ShoppingList','shopping_list_ingredients','shopping_list_id','ingredients_id')
        ->withPivot(array('unit','amount'))
        ->withTimestamps();
}  

我做错了什么?谢谢!

【问题讨论】:

    标签: php laravel many-to-many relationship


    【解决方案1】:

    你对这段代码的解释有点模糊,但重点是,你需要找到缺失的成分并将它们添加到购物清单中,对吗?

    为此,您可以使用 Collection 的 diff 方法:

    // retrieve collection of the ingredients for a recipe
    $recipe = Recipe::with('ingredients')->find($recipeId);
    
    // retrieve collection of the ingredients for a shopping list
    $list = ShoppingList::with('ingredients')->find($recipeId);
    
    // find the ingredients for the recipe that are not on the list already
    // return Collection of Ingredient models
    $missingIngredients = $recipe->ingredients->diff($list->ingredients);
    
    // and ingredients on both list and recipe
    $missingIngredients = $recipe->ingredients->intersect($list->ingredients);
    

    【讨论】:

    • 是和不是。我需要找到成分,这些成分已经在用户的购物清单上。比方说,食谱有 3 种成分(香肠、奶酪、面包)。用户创建了一个带有奶酪和香肠的列表。所以需要知道,哪些 ID 已经在列表中(ingredient->isAdded = 1 或 0)。那个更好吗? :)
    • 实际上是相同的代码,但只是改变了变量,对吧?
    • 如果您需要查找列表和配方中的成分,请使用 intersect 方法而不是 diff
    • 完美!再来一次!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多