In my database a have a table with a column called date. In the date columns are datetime entries stored. Now I want to write a query that gets me all entrys from a day.
Now If I do it like that I obviously get just the records with exact same timestamp
$date = Carbon::now()->startOfDay();
Task::where('date', $date)->get()->all();
But i need all records where the date is the same, no matter wich timestamp they have
CodePudding user response:
Laravel has a whereDate function, which is what you're looking for:
Task::whereDate('date', $date->toDateString())->get();
However, Eloquent may cause some issues since you have the column named date
, and it automatically scopes the where clauses for you. In that case, you'll need a raw command:
Task::whereRaw('DATE(`date`) = ?', [$date->toDateString()])->get();