I was looking into date filters for a project I am working on and I thought I would share the filter helper class I am now using, it may be of some use to someone out there:
// The date helper class DateFilter { public function __construct( public readonly Carbon $start, public readonly Carbon $end ) {} public static function today(): self { return new self( start: now()->startOfDay(), end: now(), ); } public static function thisWeek(): self { return new self( start: now()->startOfWeek(), end: now(), ); } } // And we can use it like this - or of course however fits your code public function getNewUsers(): NewUsersCount { return new NewUsersCount( today: User::whereCreatedBetween(DateFilter::today())->count(), thisWeek: User::whereCreatedBetween(DateFilter::thisWeek())->count(), ); }
First seen at: https://x.com/mmartin_joo/status/1671518854101475330