Filter information for the current day

Asked

Viewed 272 times

2

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 the results of today, I realized that the problem is in the filter I am only filtering the date of today date('Y-m-d') and in the created_at is 2019-05-05 13:17:08, how do I ignore the time created_at and filter by date only?

2 answers

4


In Eloquent there is the method whereDate which will consider only the date of the respective column:

Auth::user()->LogsSearch()
    ->whereDate('created_at', date('Y-m-d'));

As it has whereMonth, for just the month, whereDay, just for the day, whereYear, only for the year, and whereTime, only for the time.

  • Thank you worked perfectly !

-2

Hello, good afternoon,

You can use Carbon, which by default is already built into the Windows, 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

    What changes in the clause where when using the function strtotime or Carbon? Mainly because in both examples you kept the value of date within the where.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.