Home >Backend Development >PHP Tutorial >How to Get Last ays Record in Laravel
This tutorial will demonstrate how to get the past seven days’ records in a Laravel application. This article will briefly introduce how to get the last seven days of data in Laravel. We will walk through how to get past seven days’ records in Laravel. We will implement the Laravel code that fetches the past seven days’ data from the database.
This method works on Laravel 6, Laravel 7, Laravel 8, Laravel 9, Laravel 10 and Laravel 11 versions. You can also learn how to generate and read Sitemap XML files in this Laravel 11 tutorial.
The following is a sample code of a controller method using Carbon Laravel Eloquent to get the records of the past seven days:
Controller file:
<code class="language-php"><?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class UserController extends Controller { /** * 获取过去七天记录 * * @return \Illuminate\Http\Response */ public function index() { $date = Carbon::now()->subDays(7); $users = User::where('created_at', '>=', $date)->get(); dd($users); } }</code>
For more tutorials please visit DevScriptSchool.Com
The above is the detailed content of How to Get Last ays Record in Laravel. For more information, please follow other related articles on the PHP Chinese website!