Home > Blockchain >  Laravel Query - Count and filter
Laravel Query - Count and filter

Time:11-26

I have a problem with this query: I Want to count how many logs there are in the last 24h from a specific user, and with a specific status_code (example 400)".

Actually, with this query, I found all logs from a specific company.

  $webhookLog = WebhookLog::where('company_id', $company->id)
->orderBy('updated_at', 'desc')->get();

CodePudding user response:

In controller

in function

  $date = date('Y-m-d');
  $webhookLog = WebhookLog::where('company_id', $company->id)
  ->whereBetween('updated_at, [$date . " 00:00:00", $date . " 23:59:59"])
  ->orderBy('updated_at', 'desc')->get();

this will return todays logs

replace get() with count() to get the count.

CodePudding user response:

You can use this method for from date for 24 hours

$fromDate = Carbon::now()->subMinutes(1440);
$toDate = Carbon::now();

CodePudding user response:

Can you try this

$webhookLog = WebhookLog::query()
        ->where([
            ['company_id', '=',$company->id],
            ['user_id', '=', Auth::id()],
            ['status_code','=',400]
        ])
            ->whereBetween('created_at', [
                Carbon::now()->subHours(24),
                Carbon::now()
            ])
        ->orderBy('updated_at', 'desc')->get();
  • Related