There are a few mistakes in your query:
1 orderBy is in whereHas so it doesn't do anything 2 your first orWhere breaks the has query - it ignores keys matching for all the rest of where clauses 3 you select those rows, that have date_end in the past and date_start in the future - is it? :)
So here's what will work for you:
$specials = Product::with('specials')
->whereHas('specials', function($query) {
$query->whereNested(function ($query) {
$noDueDate = '0000-00-00 00:00:00';
$now = date('Y-m-d H:i:s');
$query
->where('date_start', $noDueDate)
->Where('date_end', $noDueDate)
->orWhere('date_start', '<', $now)
->where('date_end', '>', $now)
->orWhere('date_start', $noDueDate)
->where('date_end', '>', $now)
->orWhere('date_start', '<', $now)
->where('date_end', $noDueDate);
});
})->orderBy('id','desc')->get();
That's it man!!! Thanks a lot, yeah this is awesome, first time i am seeing $query->whereNested
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community