Mastering Laravel 12.11: Blade Imports, Typed Arrays, and Test Reliability
Overview of Laravel 12.11 Updates
Enhancing Blade with Functions and Constants
The @use directive originally simplified importing classes into views, but it now supports functions and constants. This prevents the need for messy PHP tags or fully qualified names within your markup.
@use('App\Helpers\help', 'App\Helpers\other_helper')
@use('App\Constants\UserRoles' as 'Roles')
<div>
{{ help() }}
<p>Status: {{ Roles::ADMIN }}</p>
</div>
You can also use curly brace syntax to import multiple items from the same namespace, significantly cleaning up the top of your
Type-Safe Array Getters
When working with the mixed return types. integer(), boolean(), and float() to enforce data integrity.
$data = ['user' => ['id' => 42, 'is_active' => true]];
// Returns 42 as an integer
$id = Arr::integer($data, 'user.id');
// Throws an exception if the value isn't a boolean
$active = Arr::boolean($data, 'user.is_active');
These methods ensure that if your data structure changes unexpectedly, your code fails loudly and specifically rather than passing a wrong type downstream.
Solving the 'After Response' Testing Hurdle
A common pain point involves testing jobs dispatched using dispatchAfterResponse(). Since these execute after the response is sent, standard test assertions often fail because the job hasn't run yet. The new withoutDispatchingAfterResponse() method on the Bus facade forces these jobs to run immediately during the test.
public function test_user_is_created()
{
Bus::withoutDispatchingAfterResponse();
$this->post('/register', [...]);
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
}
Syntax Notes and Best Practices
When using the new @use directive, remember that Arr::get() when the expected type is known; this aids tools like
