Laravel Select records in specific year

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel: Selecting Records by Specific Year Using `created_at` Welcome to the world of Laravel! It’s completely normal to feel a bit lost when starting with Eloquent, especially when dealing with date and time manipulation. The core of modern application development often revolves around retrieving data based on specific criteria, and filtering records by creation year using the `created_at` timestamp is a very common requirement. This guide will walk you through the most effective and performant ways to select all your `Blog` records created in a specific year, moving beyond simple fetching to robust database querying techniques. ## Understanding the Challenge You are trying to solve this: how do I filter the results of `$posts = Blog::latest()->get();` to only include entries where the creation date falls within the year 2015? While you can fetch all records and filter them in PHP, the most efficient way to handle this is to let the database engine (MySQL, PostgreSQL, etc.) do the heavy lifting. This process, known as "querying at the database level," ensures that only the necessary data is retrieved, which drastically improves performance, especially with large datasets. ## Method 1: Using `whereYear()` (The Simplest Approach) For simple year-based filtering, Laravel Eloquent provides a dedicated and very readable method called `whereYear()`. This method abstracts the underlying SQL logic for you, making your code clean and easy to maintain. If you simply want all posts from 2015: ```php use App\Models\Blog; $year = 2015; $postsFrom2015 = Blog::whereYear('created_at', $year)->latest()->get(); // $postsFrom2015 now contains only Blog records created in the year 2015. ``` **Why this works well:** This method translates directly into a highly optimized SQL query using the `YEAR()` function on your timestamp column, allowing the database to use indexes efficiently if they exist on the `created_at` column. ## Method 2: Using Date Comparisons (The Most Flexible Approach) While `whereYear()` is excellent for exact year matches, sometimes you need more complex range queries—for example, selecting all posts created *between* January 1st, 2015, and December 31st, 2015. For this, using standard comparison operators (`>=` and `<`) with `whereDate()` or raw date functions is the most flexible approach. To select records from a specific year (e.g., 2015) using range comparison: ```php use App\Models\Blog; $startOfYear = '2015-01-01'; $endOfYear = '2015-12-31'; $postsFrom2015Range = Blog::whereDate('created_at', '>=', $startOfYear) ->whereDate('created_at', '<=', $endOfYear) ->latest() ->get(); ``` **Best Practice Note:** When dealing with dates in Eloquent, always use the `whereDate()` method when comparing against a date column, as it ensures that only the date component is compared, ignoring the time component, which keeps your filtering precise. For more complex temporal logic, mastering Carbon (which Laravel relies heavily upon) is essential for handling timezones and date arithmetic correctly. As we discussed in the official documentation on [Laravel](https://laravelcompany.com), leveraging these built-in Eloquent methods leads to much cleaner and more robust code. ## Conclusion: Choosing the Right Tool For your specific need—selecting records from a single, known year—**`whereYear('created_at', $year)` is the most concise solution.** It is readable, idiomatic Laravel, and performs very well on the database side. However, if you foresee needing to filter by months, specific date ranges, or handle timezones in the future, mastering the **date comparison methods (`whereDate`, `whereBetween`)** will give you the flexibility required for advanced application development. Always prioritize querying at the database level rather than fetching everything and filtering in PHP; this is the fundamental principle behind efficient Laravel development. Happy coding!