Overview Laravel continues its rapid evolution with versions 11.41 and 11.42, introducing features that prioritize developer experience and code readability. This tutorial explores the shift from manual comparison logic to expressive, readable methods. We will examine the new **Date Shorthands** for database queries and the **Fluent Numeric Validation** rule that replaces clunky string-based validation patterns. Prerequisites To follow along, you should have a basic understanding of PHP and the Laravel framework, specifically working with Eloquent ORM and Validation. Familiarity with Carbon date objects is helpful but not required. Key Libraries & Tools - **Laravel 11.41+**: The core framework providing these new syntax updates. - **Eloquent ORM**: Laravel's database abstraction layer where the new date methods reside. - **Validator**: The component responsible for enforcing data integrity. Modernizing Database Queries with Date Shorthands Previously, querying records based on time required manual comparison operators against `now()`. While functional, it lacked semantic clarity. Laravel now provides dedicated methods to handle these common scenarios. ```python // The old way $pastEpisodes = Podcast::where('published_at', '<', now())->get(); // The new, readable way $pastEpisodes = Podcast::wherePast('published_at')->get(); ``` These shorthands extend to various temporal states: - `whereFuture('column')`: Filters for dates strictly in the future. - `whereNowOrInThePast('column')`: Includes the current timestamp in the results. - `whereToday('column')`: Filters for records occurring on the current calendar date. - `whereBeforeToday('column')` and `whereAfterToday('column')`: Perfect for handling scheduling logic without manual date formatting. Fluent Numeric Validation Rules Validation rules are moving away from pipe-delimited strings toward a fluent object-oriented approach. This change makes it easier to reference other fields and apply constraints like minimum values or integer requirements. ```python use Illuminate\Validation\Rule; $request->validate([ 'discount_price' => [ Rule::numeric() ->integer() ->min(0) ->lessThanOrEqualTo('original_price'), ], ]); ``` This syntax is significantly more maintainable than a string like `'numeric|min:0|lte:original_price'`. It allows IDEs to provide better autocompletion and prevents typos that frequently occur in long validation strings. Syntax Notes Notice the **PascalCase** naming convention for query shorthands (`wherePast`, `whereFuture`). These methods automatically handle the comparison logic and the `now()` injection under the hood. For validation, the `Rule::numeric()` method returns a fluent builder specifically designed for numerical constraints, mirroring the existing `Rule::date()` and `Rule::enum()` patterns. Tips & Gotchas - **Field References**: When using `lessThanOrEqualTo()`, ensure the field name you pass (e.g., `'original_price'`) exists within the same data array being validated. - **Time Accuracy**: Remember that `wherePast` and `whereFuture` use the current system time. Ensure your server time zone is correctly configured in `config/app.php` to avoid unexpected query results.
Validation
Software
Feb 2025 • 1 videos
High activity month for Validation. Laravel among the most active voices, with 1 videos across 1 sources.
Feb 2025
- Feb 17, 2025