How To Get Last 30 Days Record In Laravel 8

Websolutionstuff | Feb-02-2022 | Categories : Laravel PHP MySQL

in this tutorial, we see how to get last 30 days record in laravel 8. You can simply get the last 30 days record using laravel 8 eloquent model. 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.

For MySQL query, we use the INTERVAL operator. It is mainly used to calculate the date and time values. 

So, let's see get last 30 days records in laravel 6, laravel 7 and laravel 8. how to get last 30 days record in PHP, how to get last 30 days record in MySQL.

MySQL Example :

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 now() function

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

 

 

Laravel 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 Post
Featured Post
How to Get Last 15 Records in Laravel 10
How to Get Last 15 Records in...

Welcome, fellow developers! In this guide, I'll walk you through the straightforward process of fetching the latest...

Read More

Dec-08-2023

How To Create Line Chart In Laravel 9 Using Highcharts
How To Create Line Chart In La...

In this article, we will see how to create a line chart in laravel 9 using highcharts. A line chart is a graph...

Read More

Oct-04-2022

How to Reset MySQL Root User Password on Ubuntu
How to Reset MySQL Root User P...

Hey there! If you've ever found yourself scratching your head over a forgotten MySQL root password on Ubuntu, fear n...

Read More

Jan-26-2024

How To Create Unique Slug In Laravel 9
How To Create Unique Slug In L...

In this article, we will see how to create a unique slug in laravel 9. A slug is the part of a URL that i...

Read More

Sep-27-2022