问题
When I try to run the following query, it returns nothing:
Item::where(\DB::raw('date_field < NOW()'))->get()
The reason for this is, that is null
is appended to the generated MySQL query like this:
SELECT * FROM items WHERE date_field < NOW() is null;
Why does the is null
part get appended to the above query?
回答1:
This is a known issue in Laravel and has been reported on their GitHub page. Use whereRaw()
instead and pass a string:
Item::whereRaw('date_field < NOW()')->get()
回答2:
No idea why the not null
part gets appended. But I found a workaround.
Try this
Item::whereNotNull(\DB::raw('date_field < NOW()'))->get()
Of course, you may use built-in features like Carbon
Item::where('date_field', '<', Carbon\Carbon::now())->get()
回答3:
Use it this way..
Item::where('date_field','<','NOW()')->get()
回答4:
SELECT * FROM items WHERE date_field < GETDATE();
来源:https://stackoverflow.com/questions/37611461/selecting-entries-whose-date-field-now