Home >Backend Development >PHP Tutorial >How to Get Last ays Record in Laravel

How to Get Last ays Record in Laravel

Patricia Arquette
Patricia ArquetteOriginal
2025-01-20 12:09:13638browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn