DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

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;
Enter fullscreen mode Exit fullscreen mode

Read More: Laravel 8 Get Latest Record From Database


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();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)