How could i get only de date of a datetime column in laravel 5 using fluent?
I want to compare this 2015-08-30 23:00:00
with this:
$dt = Carbon::now();
$d = $dt->toDateString();//this is the date only which i want to compare
There is a mysql function called DATE which extract the date part of a date or datetime expression like this DATE(`datetime`)
but if i use it it gives me a integrity error.
Here's how i tried to use it:
$reservations = \DB::table('reservations')
->select('reservations.id','DATE(reservations.datetime)')
->get();
Nobody answered so i'll post how i solved it temporary:
Laravel provides something called Raw Expressions, so i what i did was use a raw select to get the date string of a datetime like this:
$reservations = \DB::table('reservations')
->select(\DB::raw('reservations.id, DATE(reservations.datetime)')
->get();
My goal was to compare a date from a datetime column with a Carbon
date and here's how i did it:
$dt = Carbon::now();
$d = $dt->toDateString();//Only date from datetime
$reservations = \DB::table('reservations')
->whereRaw("DATE(reservations.datetime) = '".$d."'")
->select(\DB::raw('reservations.id, DATE(reservations.datetime)')
->get();
I used whereRaw
to make a Raw sentence for the where clause.
If someone has a better way of do this same thing, maybe using Eloquent i would like to know it since Eloquent is a bit more secure...