Filter information for the current day laravel
I have the following variable in my View
:
Auth::user()
->LogsSearch()
->where('base', $linha->sigla)
->where('created_at', date('Y-m-d'))
->count()
And I want to filter today's results, I realized that the problem is in the filter I am filtering only today's date date('Y-m-d')
and in the created_at
it is 2019-05-05 13:17:08, How do I ignore the created_at
time and filter by date only?
2 answers
In Eloquent there is the method whereDate
that will consider only the date of the respective column:
Auth::user()->LogsSearch()
->whereDate('created_at', date('Y-m-d'));
Just as it has whereMonth
, for only the month, whereDay
, only for the day, whereYear
, only for the year, and whereTime
, only for the time.
Hello, Good Afternoon,
You can use carbon, which by default is already built into laravel, or you can use strtotime.
STRTOME is most recommended for something as simple as this.
$data = Auth::user()->LogsSearch()
->where('base', $linha->sigla)->where('created_at', date('Y-m-d'))->count()
$time = strtotime($data);
Now you can test in your view, just pass the variable to see the magic happen. Now if you want to use Carbon, you can simply use the following code:
$data = Auth::user()->LogsSearch()
->where('base', $linha->sigla)->where('created_at', date('Y-m-d'))->count()
$time = strtotime($data);
$minhaData = Carbon::createFromDate($data);
Don't forget to import your Carbon.