【问题标题】:Laravel wherehas ignored when using withLaravel wherehas 使用 with 时被忽略
【发布时间】:2017-09-11 10:29:59
【问题描述】:

我有一个查询,我想查看特定类型的 User 的所有 Alert(关系)项。

我正在使用以下查询

$users = User::with('alerts')->whereHas('alerts', function($q) use ($type) {
    $q->where('type', $type);
})->get();

问题是它忽略了我的 where subquery 并返回所有类型的警报,而不是我传递给 whereHas 的类型。

谢谢!

【问题讨论】:

  • whereHas 限制了您的用户结果,而不是您的警报结果。
  • 啊哈感谢您的回答,找到了解决方案,我将其添加为答案。

标签: php laravel eloquent query-builder


【解决方案1】:

解决方案

whereHas 限制用户的结果,而不是警报的结果

我必须在“with”声明中使用subquery

$users = User::whereHas('email_alerts')->with(['email_alerts' => function($q) use ($email_type) {
    $q->where('email_type', $email_type);
}, 'company'])->get();

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2017-09-04
    • 2018-10-26
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2018-03-29
    • 2019-01-22
    • 2022-01-27
    相关资源
    最近更新 更多