Laravel 8 Get Last 7 Days Records

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

For MySQL queries, we are using INTERVAL operators. It is mainly used to calculate the date and time values. In laravel, we will use the carbon subdays() function to get the last 7 days records.

So, let's see how to get the last 7 days record in laravel 8 or get the last 7 days record in MySQL query.

Get Last 7 Days Records Using MySQL Example:

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

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

 

 

Get Last 7 Days Record Using Laravel Carbon Example:

In laravel, we will use 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:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS