I want to get all the rows from a table through an expression:
table.date <= 2014-07-10
But if the column contains a datetime let's say:
2014-07-10 12:00:00
But if I do:
where('date', '<=', $date)
it won't get the row.
I guess this is because $date = 2014-07-10 which makes MySQL assume that it is 2014-07-10 00:00:00.
In regular MySQL I would just do
where DATE(date) <= $date
What would be the equivalent using Laravel's Eloquent?
If you're still wondering how to solve it.
I use
$protected $dates = ['created_at','updated_at','aired'];
In my model and in my where i do
where('aired','>=',time())
So just use the unix to compaire in where.
In views on the otherhand you have to use the date object.
Hope it helps someone!
You can use this
whereDate('date', '=', $date)
If you give whereDate then compare only date from datetime field.
Have you considered using:
where('date', '<', '2014-08-11')
You should avoid using the DATE()
function on indexed columns in MySQL, as this prevents the engine from using the index.
UPDATE
As there seems to be some disagreement about the importance of DATE()
and indexes, I have created a fiddle that demonstrates the difference, see POSSIBLE KEYS
.
You can get the all record of the date '2016-07-14' by using it
whereDate('date','=','2016-07-14')
Or use the another code for dynamic date
whereDate('date',$date)
Source: Stackoverflow.com