Selecting entries whose `date_field < NOW()`

喜欢而已 提交于 2019-12-25 15:29:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!