Laravel 5.4: How do I get records from just this week?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel 5.4: How Do I Get Records from Just This Week? Mastering Date Queries with Eloquent and Carbon As Laravel developers, we frequently need to interact with time-based data. Fetching records based on relative periods—like "this week," "last month," or "the current quarter"—is a common requirement. While Laravel provides powerful tools like Eloquent and the Carbon library, manipulating date ranges across database boundaries can often lead to confusing results if we rely solely on simple arithmetic. The initial attempts to calculate this using simple subtraction of `weekOfYear` or `day` often fail because these methods deal with calendar weeks rather than precise, inclusive date ranges suitable for SQL querying against a `created_at` timestamp column. This post will walk you through the correct, robust way to fetch records specifically from the current week, leveraging the power of Carbon and Eloquent, and exploring alternative database-level solutions. ## The Pitfall of Simple Date Arithmetic When we try to calculate dates like `$dt->weekOfYear`, we are dealing with calendar weeks. Subtracting this value from the current date gives us a date in the previous week, which is generally not what we want when querying records created *within* the current seven-day span. Database timestamps require explicit start and end boundaries for accurate filtering. Attempting to use methods like `Data::where('created_at', $dt->week)` directly often results in incorrect comparisons because Eloquent translates this into a less precise SQL comparison that doesn't account for the full date range correctly across all database systems. ## Solution 1: The Eloquent and Carbon Approach (Recommended) The most reliable, portable, and Eloquent-idiomatic way to solve this is to define the exact start date and end date of the current week using Carbon and then use the highly optimized `whereBetween` clause in your query. This approach keeps the logic within the application layer, making it easy to read and debug. We need to calculate the Monday (or Sunday) of the current week and the following Sunday (or Saturday) to define our range. Here is how you can implement this: ```php use Carbon\Carbon; use App\Models\Data; // 1. Define today's date $today = Carbon::now(); // 2. Calculate the start of the current week (assuming Monday as the start) // startOfWeek() returns the start of the week based on the locale settings. $startOfWeek = $today->startOfWeek(); // 3. Calculate the end of the current week (7 days later) $endOfWeek = $startOfWeek->copy()->addWeek(); // 4. Query records created between these two dates $dataThisWeek = Data::whereBetween('created_at', [$startOfWeek, $endOfWeek]) ->get(); // Example output check: // dd($startOfWeek->toDateString(), $endOfWeek->toDateString()); ``` **Why this works:** By calculating a precise start date and end date using Carbon's robust methods (`startOfWeek()` and `addWeek()`), we generate exact boundaries. When Eloquent translates `whereBetween` into SQL, it produces an efficient `WHERE created_at BETWEEN '...' AND '...'`, which is highly optimized by the underlying MySQL engine (or PostgreSQL, etc.). This practice aligns perfectly with Laravel's philosophy of clean data interaction, much like how you structure models in [Laravel](https://laravelcompany.com). ## Solution 2: Leveraging Native MySQL Functions (The Performance Route) For extremely large datasets where performance is paramount and you are working exclusively on a MySQL database, leveraging native SQL functions can sometimes be faster than fetching potentially large date ranges into PHP memory first. You can use MySQL's built-in date functions directly within your query. If you need records from the current week based on the MySQL `WEEK` or `YEARWEEK` functions: ```php use Illuminate\Support\Facades\DB; $currentWeekNumber = date('W'); // Get the current ISO week number (1-52) $dataThisWeekNative = Data::where(function ($query) use ($currentWeekNumber) { // This uses MySQL's WEEK function to filter records based on the week number. $query->where(DB::raw('YEARWEEK(created_at)'), $currentWeekNumber); })->get(); ``` **Note:** While this is a powerful native approach, it requires you to be very familiar with SQL syntax and relies heavily on the specific date functions provided by your database. For general application development, **Solution 1 (Eloquent/Carbon)** is generally preferred for its readability and platform independence. ## Conclusion When dealing with time-based filtering in Laravel, avoid complex arithmetic on timestamps directly within Eloquent `where` clauses. Instead, embrace Carbon to calculate precise start and end boundaries for the desired period. By using `$today->startOfWeek()` and `$today->addWeek()`, you ensure your queries are accurate, readable, and performant. For maximum flexibility, always favor explicit date range comparisons over relying on potentially ambiguous relative week calculations.