DEV Community 👩‍💻👨‍💻

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel 8 Get Last 30 Days Record From Database

In this article, we see laravel 8 get the last 30 days record from the database. In PHP, you can use INTERVAL to get the last 30 days record from the database.

Also, we can see how to get the last 1 month's data records in laravel 6/7/8 using Carbon functions and you can simply get the last 30 days record using the laravel 8 eloquent model.

For MySQL queries, we use the INTERVAL operator. It is mainly used to calculate the date and time values. And In laravel, we will use the carbon subDays() function to get the last 30 days records.

So, let's see how to get the last 30 days record in laravel 8 or laravel 8 to get the last 30 days record from the database.

Get Last 30 Days Records Using MySQL Query Example:

In this example, we will see an SQL query to get records from the last 30 days in MySQL.

select * from users where created_at > now() - INTERVAL 30 day;
Enter fullscreen mode Exit fullscreen mode

You can also use the current_date instead of the now() function.

select * from users where created_at > current_date - interval 30 day;
Enter fullscreen mode Exit fullscreen mode

Read Also: Laravel 8 Get Last 7 Days Records


Get Last 30 Days Records In Laravel Using Carbon Example:

In laravel, we are using the carbon subDays() function to get the last 30 days records.

$date = \Carbon\Carbon::today()->subDays(30);
$users = User::where('created_at','>=',$date)->get();
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
robinbastiaan profile image
Robin Bastiaan

Does the code change for versions lower than Laravel 8? You say it so specifically in your article that I am curious.

Collapse
 
techsolutionstuff profile image
Techsolutionstuff

Code is not change for lower version of laravel. It's working prefect for other version of laravel.

Thanks ✌️

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!