【问题标题】:Adding conditions to Laravel has()向 Laravel 中添加条件 has()
【发布时间】:2014-11-21 06:45:59
【问题描述】:

我有一个名为 answers 的表,它的列是 idanswersbatchcandidate_id。我想得到所有在批次号 1 下没有答案记录的候选人。

有没有办法在这个语句中添加条件(这是在我的候选模型中)?

return $this->has('answers', '=', 0)->get();

我试过这种方法,但没有用:

return $this->has('answers', '=', 0)->whereBatch(1)->get();

【问题讨论】:

  • 在文档中找到“如果您需要更多功能,您可以使用 whereHas 和 orWhereHas 方法在您的 has 查询中添加“where”条件”

标签: php laravel eloquent


【解决方案1】:

你需要whereHas:

$this->whereHas('answers', function ($q) {
   $q->whereBatch(1);
}, '=', 0)->get();

顺便说一句,这与使用闭包作为第 5 个参数调用 has 完全相同:

$this->has('answers', '=', 0, 'and', function ($q) {
   $q->whereBatch(1);
})->get();

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 2014-10-09
    • 2018-06-19
    • 2019-07-17
    • 2020-05-11
    • 2013-07-11
    • 2015-11-25
    • 2020-05-20
    • 2016-07-13
    相关资源
    最近更新 更多