Laravel released Laravel 11.3 this week which includes several exciting features designed to enhance your development workflow. These features include multi-line textarea input in Laravel Prompts, pull
and pullHidden()
methods, and hasAny
method for enhanced session management.
Multi-line Textarea Input in Laravel Prompts
Laravel 11.3 adds support for multi-line textarea inputs in Laravel Prompts. This is particularly useful when detailed text inputs are necessary, such as user bios, descriptions, or any other extended input. Here’s how you might implement it:
use function Laravel\Prompts\textarea;
$bio = textarea(
label: 'Tell us about yourself.',
placeholder: 'About me...',
required: true,
hint: 'This will be displayed on your profile.'
);
// Adding validation rules
$bio = textarea(
label: 'Tell us about yourself.',
validate: fn (string $value) => match (true) {
strlen($value) < 50 => 'Your bio must be at least 50 characters.',
strlen($value) > 5000 => 'Your bio must not exceed 5,000 characters.',
default => null
}
);
Context pull()
and pullHidden()
Methods
Laravel 11.3 also introduces pull() and pullHidden() methods for the Context service, which are useful for extracting and then removing data from the context—ideal for scenarios where transient data is used during a request’s lifecycle.
$foo = Context::pull('foo');
$bar = Context::pullHidden('foo');
These methods help manage temporary data without leaving it in the global context longer than necessary, which is particularly useful for data that is only relevant during a specific part of the application’s workflow, such as temporary user states or flash messages.
New Session hasAny()
Method
The hasAny()
method simplifies checks across multiple session variables, allowing you to confirm the presence of any listed session data efficiently. This method can clean up your code, eliminating the need for multiple has()
checks. Here’s an example:
// Before
if (session()->has('first_name') || session()->has('last_name')) {
// Perform actions
}
// After using hasAny()
if (session()->hasAny(['first_name', 'last_name'])) {
// Perform actions
}
Conclusion
These features in Laravel 11.3 offer more nuanced control over user inputs and session data, along with better management of application context. They reflect Laravel’s ongoing commitment to improving developer convenience and application robustness. For more information on all of the changes in this update, check out the official changelog.