Mysql Laravel WHERNOTIN不作为子查询使用

Mysql Laravel WHERNOTIN不作为子查询使用,mysql,database,laravel,Mysql,Database,Laravel,你们好,这么好的人 我有一个问题,我无法解决自己,即使在搜索,谷歌和阅读了这么多的文章 我有3种型号: 用户 食品 成分 User.php public function favourites() { return $this->belongsToMany(Food::class, 'food_user', 'user_id', 'food_id', 'id', 'id'); }` // Some people dislike some ingredient(

你们好,这么好的人

我有一个问题,我无法解决自己,即使在搜索,谷歌和阅读了这么多的文章

我有3种型号:

  • 用户

  • 食品

  • 成分

    User.php
    
    public function favourites()
    {    
           return $this->belongsToMany(Food::class, 'food_user', 'user_id', 'food_id', 'id', 'id');
    }`
    
    // Some people dislike some ingredient(s) or alergic to it
    public function dislikes()
    {
           return $this->belongsToMany(Ingredient::class, 'ingredient_user', 'user_id', 'ingredient_id', 'id', 'id');
    }
    
    Food.php
    
    public function likes()
    {
           return $this->belongsToMany(User::class, 'food_user', 'food_id', 'user_id', 'id', 'id');
    }
    
    public function ingredients()
    {
           return $this->belongsToMany(Ingredient::class, 'food_ingredient', 'food_id', 'ingredient_id', 'id', 'id');
    }
    
    Ingredient.php
    
    public function foods()
    {
           return $this->belongsToMany(Food::class, 'food_ingredient', 'ingredient_id', 'food_id', 'id', 'id');
    }
    
    public function disliked()
    {
           return $this->belongsToMany(User::class, 'ingredient_user', 'ingredient_id', 'user_id', 'id', 'id');
    
  • }

    所以在设置了这些关系之后,现在我想实现搜索系统

    SearchController.php
    
    use ...
    use ...
    
    public function foods(Request $request)
    {
        $user = User::with(['dislikes'])->find(x);
    
        $dislikes = $user->dislikes->pluck('pivot')->pluck('ingredient_id')->toArray();
    
        $query = $request->query('keyword');
    
        $builder = Food::query()->with(['ingredients']);
    
        $builder->where('name', 'LIKE', "%$query%");
    
        $builder->orWhereHas('ingredients', function ($query) use ($dislikes) {
            // This is not working, it STILL fetch all food with ingredients the current user dislikes
            $query->whereNotIn('ingredient_id', $dislikes);
        });
    
        $foods = $builder->get();
    
        return ...
    }
    
    知道为什么
    whereNotIn()
    不在
    或wherehas()子句中工作吗


    提前感谢您

    替换WhereHas by WhereHas自您使用whereNotIn以来

    替换WhereHas by WhereHas自您使用whereNotIn以来

    您必须更改您的查询,因为with将返回与您的情况不同的所有结果

    $builder = Food::query()->with(['ingredients'=>function ($query) use ($dislikes) {
         $query->whereNotIn('ingredient_id', $dislikes);
         }]); 
        $builder->where('name', 'LIKE', "%$query%"); 
        $foods = $builder->get(); 
    
    您可以使用带有关系的内部回调,也可以在如下回调时使用

    $builder = Food::query()->when(count($dislikes),function ($q) use ($dislikes){
     $q->whereHas('ingredients',function ($query) use ($dislikes) {
     $query->whereNotIn('ingredient_id', $dislikes);
     }); 
    }); 
    $builder->where('name', 'LIKE', "%$query%"); 
    $foods = $builder->get(); 
    

    您必须更改查询,因为with将返回案例中的所有条件结果

    $builder = Food::query()->with(['ingredients'=>function ($query) use ($dislikes) {
         $query->whereNotIn('ingredient_id', $dislikes);
         }]); 
        $builder->where('name', 'LIKE', "%$query%"); 
        $foods = $builder->get(); 
    
    您可以使用带有关系的内部回调,也可以在如下回调时使用

    $builder = Food::query()->when(count($dislikes),function ($q) use ($dislikes){
     $q->whereHas('ingredients',function ($query) use ($dislikes) {
     $query->whereNotIn('ingredient_id', $dislikes);
     }); 
    }); 
    $builder->where('name', 'LIKE', "%$query%"); 
    $foods = $builder->get(); 
    

    dd($dislikes)你得到的不喜欢的成分ID是什么:
    [3,7,10]
    好的。请让我再次分析你的查询。因为它看起来很好,试一下或者从哪里打印出qurey如果它不起作用,那么你就会知道哪种情况失败了dd($dislikes)你得到的不喜欢的成分ID是什么:
    [3,7,10]
    好的。请让我再次分析您的查询。因为它看起来很好,请尝试或在何处打印qurey如果它不起作用,那么您会知道哪个案例失败Hello感谢您的回答,但我恐怕您迟到了,我很抱歉您好谢谢您的回答,但我恐怕您迟到了,我是sorry@ChristianDelvianto.no问题。很高兴你解决了你的问题issu@ChristianDelvianto.no问题。很高兴你已经解决了你的问题