How to get all records for current year?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Get All Records for the Current Year in Laravel Eloquent

As developers working with relational databases via an ORM like Eloquent, querying data based on time periods—especially calendar years—is a very common requirement. You often need to pull reports, aggregate data, or filter records based on when they were created. While the initial thought might seem straightforward, handling date and time comparisons correctly in SQL and Eloquent requires precision.

This post will walk you through the most robust and efficient ways to retrieve all records from your database that fall within the current calendar year.

The Pitfall of Simple Equality Checks

A common starting point for this task is trying to use a simple equality comparison:

// This approach often fails or yields incorrect results in real-world scenarios
$currentYear = date('Y');
$articles = Article::where('created_at', $currentYear)->get();

While it looks simple, relying on direct equality (=) with a full timestamp column like created_at is problematic. The created_at column stores the full date and time (e.g., 2024-07-15 14:30:00). Comparing it directly to just the year string ('2024') will usually result in zero matches because the database expects a full date/time comparison, not a partial match.

To accurately select records for an entire year, we need to define a range—a start date and an end date. This is the professional approach that ensures correctness across all time zones and data types.

The Correct Eloquent Approach: Using Date Range Operators

The most reliable way to solve this is by using the whereBetween method provided by Eloquent, which allows you to specify a range for a date or datetime column. We calculate the start of the current year and the start of the next year to define our boundary precisely.

Step-by-Step Implementation

First, determine the boundaries:

  1. Find the first moment of the current year (January 1st).
  2. Find the first moment of the next year (January 1st of the following year).

This method ensures that every record created between January 1st of the current year and December 31st of the current year is included, regardless of the time component.

use Illuminate\Support\Carbon;

// 1. Define the start and end dates for the current year
$startOfYear = Carbon::now()->startOfYear();
$endOfYear = Carbon::now()->endOfYear();

// 2. Query the database using whereBetween
$currentYearArticles = Article::whereBetween('created_at', [$startOfYear, $endOfYear])->get();

// Output the results
dd($currentYearArticles);

Explanation of Best Practices

By using Carbon—Laravel’s powerful date/time library—we leverage built-in methods like startOfYear() and endOfYear(). This makes the code highly readable, timezone-aware, and significantly reduces the chance of off-by-one errors compared to manually calculating dates. This focus on leveraging robust libraries aligns perfectly with the principles of building scalable applications, much like those promoted by resources found on laravelcompany.com.

Advanced Alternative: Using Database Functions (For Performance)

If you are dealing with extremely large datasets and performance is a critical concern, pushing the filtering logic down to the database itself can be more efficient than fetching potentially millions of rows into PHP memory. You can achieve this using database-specific date functions.

For example, in MySQL, you could use YEAR():

use Illuminate\Support\Facades\DB;

$currentYear = date('Y');

$articles = DB::table('articles')
    ->whereYear('created_at', $currentYear)
    ->get();

While this method is concise, it requires you to be aware of the specific SQL functions supported by your database (MySQL, PostgreSQL, etc.). For most standard Laravel applications, the Eloquent range approach demonstrated above offers a good balance between readability and performance.

Conclusion

To reliably fetch all records for the current year in Laravel, always favor date range comparisons using whereBetween combined with PHP's powerful Carbon library. This practice ensures your data queries are accurate, timezone-safe, and maintainable. By adopting these structured approaches, you write code that is not only functional but also adheres to senior-level development standards, making your application more robust and scalable.