Overview Laravel 11.2 introduces a suite of developer-centric tools designed to streamline data manipulation and application configuration. This release emphasizes code readability and centralized management, specifically through the introduction of the `fluent` helper and more flexible scheduling options within the application bootstrap process. These updates reduce boilerplate and make your codebase feel more expressive and maintainable. Prerequisites To follow this guide, you should be comfortable with PHP 8.2+ and have a baseline understanding of Laravel fundamentals, including Artisan commands, basic testing patterns, and Laravel Collections. Key Libraries & Tools * **Laravel 11**: The core framework providing the new context and fluent helpers. * **Artisan CLI**: Laravel's built-in command-line interface for running tasks and tests. * **PHPUnit**: The testing framework used to verify command output assertions. Code Walkthrough The Fluent Helper Previously, accessing nested array data required clunky array syntax or wrapping data in a collection. The `fluent()` helper allows for seamless method chaining and dot-notation access. ```php $data = ['user' => ['name' => 'Felo', 'posts' => [['title' => 'Post 1']]]]; // Direct property access $name = fluent($data)->user->name; // Chaining collection methods $titles = fluent($data)->collect('user.posts')->pluck('title'); ``` The `fluent` object acts as a bridge, allowing you to use `get()`, `collect()`, or even dynamic property calls to extract deeply nested values without repetitive `isset()` checks. Scoping and Serialization Laravel now provides a `scope` method for the fluent helper. This isolates a portion of your data so you can perform operations like JSON serialization directly on a specific sub-key. ```php $addressJson = fluent($data)->scope('user.address')->toJson(); ``` Scheduling in Bootstrap Traditionally, scheduled tasks lived in `routes/console.php`. You can now define these directly in `bootstrap/app.php` using the `withSchedule` method. This centralizes your app's configuration, keeping routing, middleware, and scheduling in one logical file. ```php // bootstrap/app.php ->withSchedule(function (Schedule $schedule) { $schedule->command('backup:run')->daily(); }) ``` Syntax Notes * **Dot Notation**: Use strings like `user.profile.id` within `get()` or `scope()` methods to traverse arrays. * **Method Proxying**: The fluent helper proxies property calls (e.g., `->user`) to its internal `get()` method for cleaner syntax. Practical Examples * **API Response Parsing**: Use `fluent($apiResponse)->collect('data')->map(...)` to quickly process external data. * **Testing Silent Commands**: Use `$this->artisan('my:command')->doesntExpectOutput();` to verify that a command runs silently when no work is pending. Tips & Gotchas * **Context Helper**: Remember that the `context()` global helper requires an array; it's a shorthand for the `Context` facade used to track metadata across your application's execution cycle. * **Organization**: While you can move scheduling to `bootstrap/app.php`, keep it clean. If your schedule grows too large, sticking with `routes/console.php` might prevent your bootstrap file from becoming a "junk drawer."
Laravel Collections
Software
Apr 2024 • 1 videos
High activity month for Laravel Collections. Laravel among the most active voices, with 1 videos across 1 sources.
Apr 2024
- Apr 9, 2024