How to select min and max in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Aggregation in Laravel: How to Select MIN and MAX Values Efficiently

As senior developers, we often bridge the gap between raw SQL power and the elegant abstraction provided by frameworks like Laravel. When working with database operations, specifically retrieving aggregate values like MAX() or MIN(), it’s common to run into syntax hurdles when translating direct SQL into the Eloquent Query Builder methods.

This post will walk you through the confusion you encountered regarding selecting minimum and maximum values in Laravel, providing a correct, robust, and idiomatic solution. We will demystify why your initial attempt failed and show you the best practices for handling data aggregation in a Laravel application.

The SQL Foundation vs. Laravel Implementation

Let's start by looking at the raw SQL query you were trying to achieve:

SELECT MAX(date_start) AS DateStart, MIN(date_end) AS DateEnd FROM DBTest;

This is perfectly valid SQL. It asks the database to calculate the single maximum value for date_start and the single minimum value for date_end across the entire table, returning a single row of results.

When moving this logic into Laravel's Query Builder (DB facade), we need to tell Laravel exactly how to construct that raw aggregation. Your attempt:

$data = DB::table('DBTest')
    ->select(max('date_start'), min('date_end')) // This caused the error
    ->get();

Why the Error Occurred: Understanding the Limitation

The error message you received—max(): When only one parameter is given, it must be an array—is critical. It tells us that Laravel's select() method, when used in this context, expects standard column references or specific expressions, not direct application of aggregate functions like max() and min() as standalone functions within the selection list.

In many scenarios, applying aggregate functions directly inside select() on a table builder doesn't automatically handle the necessary grouping or aggregation structure required by the underlying SQL engine for these operations across the entire result set in this manner.

The Correct Laravel Solutions for Aggregation

To successfully retrieve these aggregated values in Laravel, we must utilize methods that allow us to inject raw SQL expressions directly into the query builder. There are two primary, robust ways to achieve your goal: using selectRaw() or leveraging the underlying database functions via DB::raw().

Method 1: Using selectRaw() (Recommended for Simplicity)

The selectRaw() method is designed precisely for injecting arbitrary SQL snippets into your query. This is often the cleanest way when you are performing a single, specific aggregate calculation.

use Illuminate\Support\Facades\DB;

$results = DB::table('DBTest')
    ->selectRaw('MAX(date_start) AS DateStart, MIN(date_end) AS DateEnd')
    ->first(); // Use first() since we expect only one aggregated row

// $results will contain the calculated max and min dates.

Why this works: By using selectRaw(), you are telling Laravel: "Do not try to interpret these as PHP functions; treat this entire string as the SQL command to be executed directly against the database." This bypasses the ambiguity that caused the initial error.

Method 2: Using DB::raw() (For Complex Expressions)

Alternatively, you can use the DB::raw() helper, which is useful when you need more complex expressions or calculated fields that involve mixing column names and functions.

$results = DB::table('DBTest')
    ->select([
        DB::raw('MAX(date_start) AS DateStart'),
        DB::raw('MIN(date_end) AS DateEnd')
    ])
    ->first();

This method is slightly more verbose but offers greater control over the construction of the final selection list.

Best Practices for Data Retrieval in Laravel

When dealing with aggregation, remember that the power lies in letting the database do the heavy lifting. As you build complex data interactions in Laravel, whether using Eloquent or the Query Builder, always favor methods that allow you to express your intent clearly. For advanced query building and managing relationships, exploring documentation like the official Laravel documentation will provide deeper insights into optimizing these operations.

Conclusion

The confusion surrounding max() and min() in Laravel is a classic example of the difference between PHP method calls and SQL expression injection. Never try to pass aggregate functions directly as simple PHP functions inside select(). Instead, embrace methods like selectRaw() or DB::raw() to safely inject your desired SQL logic. By understanding this distinction, you write more robust, readable, and maintainable code that efficiently leverages the power of your database.