Home > Net >  Laravel data monthly count
Laravel data monthly count

Time:02-01

I want to display how many records were inserted every month, today is Feb 1, it should be Zero but it shows 180?

enter image description here

Card::make(
  'Incidents this month',
  IncidentReport::where('created_at', '>', now()->subDays(30))->count(),
),

CodePudding user response:

use whereBetween and Carbon::now()->startOfMonth() and Carbon::now()->endOfMonth()

IncidentReport::whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()])->count();

The issue is you are subtracting days from today's day.

now()->subDays(30)

This will return 01-Jan-2023

Ref: Carbon

  • Related