I'm experiencing a problem with the orWhereHas method using with a relation.
It looks like an old bug:
https://github.com/laravel/framework/issues/7207
time() is a hasMany() relationship.
$times = $user->time()
->whereHas('sample_relation', function ($query) {
$query->whereDate('created_at', date('Y-m-d', time()));
})
->orWhereHas('sample_relation_2', function($query) {
$query->whereDate('created_at', date('Y-m-d', time()));
})
->get();
The problem is that AND has precedence over OR. You have to nest the constraints:
$times = $user->time()
->where(function ($query) {
$query->whereHas('sample_relation', function ($query) {
$query->whereDate('created_at', date('Y-m-d', time()));
})
->orWhereHas('sample_relation_2', function($query) {
$query->whereDate('created_at', date('Y-m-d', time()));
});
})
->get();
Describe your issue properly.
What is expected and what your query actually do.
Is sample_relation_2 different from sample_relation ?
@staudenmeir Thank you for the answer.
@rSammy Yes, sample_relation_2 is different. The problem is solved.
Most helpful comment
The problem is that
ANDhas precedence overOR. You have to nest the constraints: