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;

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

select * from users where created_at > current_date - interval 30 day;

 

 

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();

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS