laravel-sql查询

vm0i2vca  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(294)

我有一个查询,可以获取主事人员的凭证,尽管如果用户有任何凭证,它可以返回许多行。
我试图得到一个查询,如果其中一行 renew 列设置为 1 然后忽略它,否则,如果它仍然存在,则返回结果 0 ```
$officiants = Officiant::where('status', 1)
->whereHas('cred',function($q) {
$q->where('renewal', 0);
})
->get();

cl25kdpy

cl25kdpy1#

$officiants = Officiant::where('status', 1)
        ->whereHas('cred',function($q) {
            $q->where('renewal', 0);
        })->with([‘cred’=> function($query) {
                $query->where('renewal', 0);
           })
        ->get();

如果你想得到有信誉的官员,那么你需要这个查询。

0md85ypi

0md85ypi2#

你必须使用 orWhere() 它也将获取更新为1的行

$officiants = Officiant::where('status', 1)
            ->whereHas('cred',function($q) {
                $q->where('renewal', 0);
                 $q->orWhere('renewal',1);    
            })

            ->with(['cred' => function($q) {
                $q->where('renewal', 1);
                $q->where('renewal', 0); 
            }])
            ->get();

相关问题