我有 5 个数据库行client_id
,其中 3 个标记为completed, Yes
。
此代码按预期获得 3 个结果:
$indGoal = $client->indGoal()->where('completed','=','Yes')->get();
这段代码没有结果:我期望 2。
$indGoal = $client->indGoal()->where('completed','!=','Yes')->get();
这个问题建议添加->orWhereNull('completed')
- 有效,但忽略了这种client_id
关系。该请求带来了所有非Yes
结果,无论$client
我的客户端模型供参考:
public function indGoal()
{
return $this->hasMany('App\Models\IndGoal');
}
您应该orWhere
在回调中对过滤器进行分组,这样它们就不会干扰现有的过滤器。
$indGoal = $client->indGoal()
->where(function ($query) {
$query->orWhere('completed', '!=', 'yes')
->orWhereNull('completed');
})
->get();
这样,查询构建器就知道任何分组条件都应该为真,所有其他条件都是独立的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句