Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jesse_orange_newable's avatar

Getting data between date ranges

In Laravel when you make a migration by default you generally get two timestamps: created_at and updated_at which is a nice to have.

In my application if I wanted to get say, all the users who registered in the last month individually, or say, a count of these users, how would that be constructed?

I ask because I read that a controller should try to stick to the CRUD methods, but I don't see how this is possible in such a situation.

Basically I'm thinking of having a stats controller for things like:

  • registrations
  • activities
  • etc

In such a scenario would it be more scale-able just to select a month and have something like the following:


$users = User::whereMonth('created_at', $request->get('month'))->get();

Or would you use a job and just collate stats automatically?

I'm feeling this is actually dead simple but I'm floundering.

0 likes
1 reply
ollie_123's avatar
Level 6

Hey @jesse_orange_newable

Carbon is great for this. You could use something like:

$date = \Carbon\Carbon::today()->subDays(30);

        $userRegisteredCount = \App\User::where('created_at', '>=', $date)
                    ->where('status', 'approved')
                    ->count();

$userRegistered = \App\User::where('created_at', '>=', $date)
                    ->where('status', 'approved')
                    ->get();

Then in your view you would use {{ $userRegisteredCount }}

Or if you wanted to list them you could change it to ->get()

@foreach ($userRegistered as $registered)

{{ $userRegistered->name }}

@endforeach

You can then just pass the variable inside compact in your view like so:-

return view(
            'your_view_name', 
            compact
            (
                'userRegisteredCount'
        'userRegistered'
            )
        );

Please or to participate in this conversation.