Laravel 10.35 Released
Laravel 10.35 Released: New Features and Updates
The Laravel team released v10.35 with a Blade @use directive, a number abbreviation helper, the ability to generate a secret with artisan down, and more. Here is a bit more info about the new features introduced this week:
Here's a breakdown of the new features in Laravel 10.35:
1. Blade @use Directive:
This directive allows you to import PHP classes directly into your Blade templates without using raw PHP tags. This makes your code cleaner and more readable.
Benefits:
- Improved Readability: You can avoid writing raw PHP code within your Blade templates, making them more readable and maintainable.
- Reduced Clutter: You can import multiple classes at once, reducing the need for repeated
@phpblocks. - Centralized Organization: You can organize your code by importing classes from separate files, promoting better code structure.
Example:
{-- Before --}}
@php
use \App\Enums\WidgetStatusEnum as Status;
@endphp
{{-- After --}}
@use('App\Enums\WidgetStatusEnum', 'Status')
@use('App\Models\Bar')
{{ Status::Foo }}
{{ Bar::first() }}
2. Number Abbreviation:
The Number::abbreviate() method converts large numbers into human-readable formats, like "1M" for 1 million. This improves the display and readability of large numbers in your application.
Benefits:
- Concise Representation: Large numbers are displayed in a compact and easily understandable format.
- Enhanced User Experience: Users can easily grasp the magnitude of large numbers without needing to decipher complicated formats.
- Improved Accessibility: Abbreviated numbers are easier to read for users with visual impairments.
Example:
Number::abbreviate(1_000_000); // "1M"
Number::abbreviate(100_001); // "100K"
Number::abbreviate(99_999); // "100K"
Number::abbreviate(99_499); // "99K"
3. Secret Generation for Artisan Down:
The artisan down command now accepts the --with-secret option to automatically generate a secret key for bypassing maintenance mode. This simplifies the process and avoids the need to manually define a secret.
Benefits:
- Simplified Maintenance: You can quickly enable maintenance mode without needing to define a secret key.
- Enhanced Security: Generated secrets are more secure than manually defined ones, reducing the risk of unauthorized access.
- Convenience: It saves time and effort compared to manually defining and managing secrets.
Example:
php artisan down --with-secret
4. Conditional Assertions in AssertableJson:
The AssertableJson class now supports the Conditionable trait, allowing you to write conditional assertions within a single chain of assertions. This improves the clarity and conciseness of your test code.
Benefits:
- More Readable Tests: You can write conditional assertions without needing nested blocks, making your tests more readable and easier to follow.
- Reduced Code Duplication: You can avoid repeating the same assertion logic within multiple conditions.
- Improved Test Maintenance: Maintaining your test code becomes easier with a single assertion chain for different conditions.
Example:
// Before
$response->assertJson(function (AssertableJson $json) use ($condition) {
$json->has('data');
if ($condition) {
$json->has('meta');
}
$json->etc();
});
// After
$response
->assertJson(fn (AssertableJson $json) => $json->has('data'))
->when($condition, fn (AssertableJson $json) => $json->has('meta'))
// ...
;
Additional Resources:
- Laravel 10.35 Release Notes: https://github.com/laravel/laravel/releases
- Video on Blade @use Directive: https://m.youtube.com/watch?v=wFvyjwRPaUE
0 Comments
Like 0