Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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();

Last updated 2 years ago.
0

That's it man!!! Thanks a lot, yeah this is awesome, first time i am seeing $query->whereNested

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

krsman krsman Joined 30 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.