Exploring New Laravel Features: From Lateral Joins to Enum Validation

Laravel////3 min read

Overview

Modern development requires tools that balance performance with developer experience. continues to refine its ecosystem, recently introducing updates in versions 10.44 and 10.45 that streamline data manipulation and validation. These updates focus on providing more granular control over eloquent collections and database query logic.

Prerequisites

To follow this guide, you should have a solid grasp of PHP 8.x and the framework. Familiarity with Eloquent ORM, basic SQL join logic, and Validation Rules is necessary to understand the implementation details.

Key Libraries & Tools

  • Laravel Framework: The primary PHP framework hosting these features.
  • Eloquent ORM: Laravel's built-in database mapper used for the lateral join examples.
  • PHP Enums: Utilized for the updated validation rules.

Code Walkthrough

Eloquent Collection Select

The select method now extends to Eloquent collections, allowing you to filter specific attributes after fetching data. Unlike pluck, which returns a single column, select maintains the collection structure with multiple keys.

$users = User::all();
$filtered = $users->select(['name', 'email']);

Enum Validation Rules

Laravel's enum validation now includes only and except methods. This allows you to restrict valid enum cases within specific requests without creating multiple enum classes.

Rule::enum(Status::class)->only([Status::Published]);

Manual Rate Limit Incrementing

The rate limiter now supports a custom increment value. Instead of a simple hit(), you can use increment() to penalize specific actions more heavily.

RateLimiter::increment($key, $seconds = 60, $amount = 3);

Lateral Joins in Eloquent

Lateral joins act like a foreach loop within SQL. They allow you to run a subquery for each row of the main query, enabling limits on joined results—a feat traditionally difficult with standard joins.

$customers = DB::table('customers')
    ->joinLateral(
        DB::table('sales')
            ->whereColumn('sales.customer_id', 'customers.id')
            ->orderBy('created_at', 'desc')
            ->limit(1),
        'latest_sale'
    )->get();

Syntax Notes

The joinLateral method accepts a closure or a query builder instance as its first argument and an alias as the second. It transforms the subquery into a correlated subquery that can reference columns from the outer table.

Practical Examples

Use joinLateral when you need to retrieve the "latest N records" for every user in a single query. Use the updated increment rate limiter for security-sensitive endpoints where a failed attempt should count as multiple hits toward a lockout.

Tips & Gotchas

When using select on collections, remember it creates a new collection; it does not modify the existing one in place. For lateral joins, ensure your version supports the syntax, as this is a relatively modern SQL feature.

Topic DensityMention share of the most discussed topics · 6 mentions across 5 distinct topics
33%· products
17%· people
17%· products
17%· products
17%· people
End of Article
Source video
Exploring New Laravel Features: From Lateral Joins to Enum Validation

Lateral Join, Custom Rate Limit Increase, Except/Only Enum Rule & More

Watch

Laravel // 7:23

The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.

Who and what they mention most
3 min read0%
3 min read