What is the best way to structure the following database query in Laravel

I am really having trouble constructing a query that meets the following criteria.

Where the start date is today/future OR 0000/00/00
Where the end date is today/future OR 0000/00/00
Where views more than 3

Here is what I have so far.

$users = DB::table('videos') ->where('start' <= date('Y-m-d')); ->orwhere('start' = "0000:00:00 0000"); ->where('end' > date('Y-m-d')); ->orwhere('end' = "0000:00:00 0000"); ->where('views' => "3"); ->get();

Any advice would be appreciated, thanks guys!

--------------Solutions-------------

You want to use advanced where with parameter grouping:

$users = DB::table('videos')
->where(function($q) {
$q->where('start', '<=', date('Y-m-d'))
->orWhere('start', '=', "0000:00:00 0000");
})
->where(function($q) {
$q->where('end', '>', date('Y-m-d'))
->orWhere('end', '=', "0000:00:00 0000");
})
->where('views', '>', 3)
->get();

Your query has too little brackets to produce usable code:

Your query looks like:

SELECT * FROM TABLE WHERE start<somedate OR start='0000:00:00 0000' AND
end>somedate OR end='0000:00:00 0000' etc.

Your question definition however requires the query to look like:

SELECT * FROM TABLE WHERE (start<somedate OR start='0000:00:00 0000') AND
(end>somedate OR end='0000:00:00 0000') etc.

So add the brackets and you probably get the result you are looking for.

Category:php Time:2017-12-04 Views:2

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.639 (s). 13 q(s)