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 Run Specific Seeder in Laravel 8
How to Run Specific Seeder in...

In this example, we will learn how to run a specific seeder in laravel 8. If you want to run only one seeder in laravel...

Read More

Jan-19-2022

Cron Job Scheduling In Laravel
Cron Job Scheduling In Laravel

In this article, we will see cron job scheduling in laravel. Many times we require to run some piece of code in a specif...

Read More

Sep-28-2020

Optimizing Laravel 11 for Serverless Deployment on AWS Lambda
Optimizing Laravel 11 for Serv...

In this guide, I'll walk you through how to optimize a Laravel 11 application for serverless deployment on AWS Lambd...

Read More

Sep-25-2024

How To Disabled Submit Button After Clicked Using jQuery
How To Disabled Submit Button...

In this article, we will see how to disabled submit button after clicked using jquery. Using jQuery we will disable the...

Read More

May-08-2022