首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不作为子查询的Laravel whereNotIn

问不作为子查询的Laravel whereNotIn
EN

Stack Overflow用户
提问于 2021-06-03 08:59:42
回答 1查看 107关注 0票数 0

你好,好棒的苏人!

我有一个问题,即使在搜索、搜索和阅读了这么多文章之后,我也无法自己解决。

我有三个模特:

  1. User

  1. Food

  1. Ingredient

User.php公共函数宠儿(){返回$this->归属to (食品:类、'food_user‘、'user_id’、'food_id‘、'id’、'id');} //一些人不喜欢某些成分或对其不喜欢的公共函数(){){返回$this--归属于to(:类、成分_user、'user_id‘、’user_id‘、’function _id‘、'id’、'id');} Food.php公共函数喜欢(){返回$this->归属于this(用户::class,'food_user','food_id','user_id','id','id');}公共函数成分(){返回$this->属于this(成分::class,‘food_ function’,'food_id','id');} Ingredient.php公共功能食品(){返回$this->归属于this(食品::class,“食品_配料”,“配料_id”,‘食品_id’,'id','id');}不喜欢的公共函数(){返回$this->归属于disliked(用户::类,‘配料_用户’,‘成分_id’,'id');

}

因此,在设置了这些关系之后,现在我想实现搜索系统。

代码语言:javascript
复制
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()为什么不在orWhereHas()子句中工作吗?

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-06-03 09:09:24

将orWHereHas替换为WhereHas,因为您使用whereNotIn

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67818448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档