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?

Author: novic, 2019-05-05

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.

 3
Author: Woss, 2019-05-05 18:02:28

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.

 -1
Author: rickandrade, 2019-05-05 17:55:16