【问题标题】:Using orWhere with eloquent使用 orWhere 与 eloquent
【发布时间】:2021-10-11 16:22:48
【问题描述】:

我有表格: courriers(courier_id, repondre ..) 和 reponse(reponse_id, structure, courrier_id) 由一对多关系链接 (1个快递员可以有多个reponses) 我想找到 courriers.repondre = 1 且没有响应的快递公司或有响应和 reponses.structure 'INFO' 的快递公司 我怎么能用 Eloquent 做到这一点 我正在尝试:

$courriers = Courrier::where('repondre', '=', 1)
    ->doesntHave('reponses')
    ->orWhere('reponses.structure', '<>', 'DMO')
    ->get(); 

条件(repondre,'=','1')两种情况都要验证

【问题讨论】:

    标签: php sql laravel eloquent laravel-8


    【解决方案1】:

    前两个条件必须组合在一起才能使您的查询起作用。

    Courrier::where('repondre', 1)
        ->where(function ($query) {
            $query->doesntHave('responses')
                  ->orWhereHas('responses', function ($query) {
                      $query->where('structure', '<>', 'DMD');
                  });
        })
        ->get()
    

    【讨论】:

    • 条件(repondre,'=','1')两种情况都要验证
    • 我将编辑我的答案。基本上将 where('repondre', 1) 移出 where 闭包并将 orWhereHas 移到里面。
    猜你喜欢
    • 2020-10-03
    • 2020-08-03
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2021-07-12
    • 2018-01-30
    相关资源
    最近更新 更多