How To Get Last 7 Days Record In Laravel 8

Websolutionstuff | Jan-31-2022 | Categories : Laravel PHP MySQL

In this example, we will see how to get the last 7 days record in laravel 8. You can simply get the last 7 days record using laravel 8 eloquent model. In PHP, you can use INTERVAL to get the last 7 days record from the database table. Also, we can see how to get last week's records in laravel 6/7/8 using Carbon functions.

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

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

 

MySQL Example :

SQL query to get records from the last 7 days in MySQL.

select * from users where created_at > now() - INTERVAL 7 day;

 

 

Laravel Example :

In laravel, we are using the carbon subdays function to get the last 7 days records.

$date = \Carbon\Carbon::today()->subDays(7);
$users = User::where('created_at','>=',$date)->get();

 


You might also like :

Recommended Post
Featured Post
How To Get Data Between Two Dates In Laravel 9
How To Get Data Between Two Da...

In this article, we will see how to get data between two dates in laravel 9. Here we will learn how to count d...

Read More

Dec-19-2022

Laravel 8 Has Many Through Relationship Example
Laravel 8 Has Many Through Rel...

In this example we will see laravel 8 has many through relationship example. hasManyThrough relationship difficult to un...

Read More

Nov-17-2021

Laravel 9 Paypal Payment Gateway Integration
Laravel 9 Paypal Payment Gatew...

In this article, we will see laravel 9 paypal payment gateway integration. Here, we will learn how to integrate the...

Read More

Jan-17-2023

How To Import Large CSV File In Database Using Laravel 9
How To Import Large CSV File I...

In this article, we will see how to import a large CSV file into the database using laravel 9. Here, we will learn&...

Read More

Sep-15-2022