The Laravel team released 9.42 this week with singleton route resources, conditional report helpers, and more:
Thanks to @jessarchercodes, today’s Laravel release allows you to quickly define routes for “singleton” resources.
These are resources like a user “profile” that have one and only one instance in your application.
📕 Read more in the docs: https://t.co/IyAvu0eYWz pic.twitter.com/WSD4vgSiBY
— Taylor Otwell 🪐 (@taylorotwell) November 29, 2022
Define routes for singleton resources
Jess Archer contributed a Route::singleton()
method to register routes for singleton resources. These are resources that are either “one of one” or “zero or one” resources. For example, a profile
endpoint:
1Route::singleton('profile', ProfileController::class);
2/*
3Defines:
4GET /profile
5GET /profile/edit
6PUT/PATCH /profile
7DELETE /profile
8*/
9
10// Nested resource
11Route::singleton('videos.thumbnail', VideoThumbnailController::class);
12
13/*
14Defines:
15GET /videos/{video}/thumbnail
16GET /videos/{video}/thumbnail/edit
17PUT/PATCH /videos/{video}/thumbnail
18DELETE /videos/{video}/thumbnail
19*/
20
21// Only and except
22Route::singleton('profile', ProfileController::class)
23 ->except('destroy');
24
25Route::singleton('profile', ProfileController::class)
26 ->only(['show', 'update']);
The make:controller
command was also updated to include --singleton
and --creatable
flags to generate singleton controllers.
Check out the Singleton Resource Controllers docs for more examples and details. Excellent work, Jess!
Added a rest option for queue:listen
@PHPGuus contributed a --rest=
option to the queue:listen
command that will sleep for the number of seconds passed to rest:
1php artisan queue:listen --rest=5
Determine if a string is a ULID
Michael Nabil contributed an isUlid()
method to the Stringable
class to check if a string is a valid Universally Unique Lexicographically Sortable Identifier (ULID) string. You can use it as follows:
1use Illuminate\Support\Str;
2
3Str::isUlid('01GJSNW9MAF792C0XYY8RX6QFT'); // true
4Str::of('01GJSNW9MAF792C0XYY8RX6QFT')->isUlid(); // true
Conditional report helpers
Michael Nabil contributed two new conditional report()
helpers. These might be useful to shorten checks where you might report when a condition is true
or false
:
1// Normal report usage
2if(! Auth::user()->isAdmin()){
3 report('Is route need permission user');
4}
5
6// Shorten/avoid `if` with:
7report_if(
8 Auth::user()->isAdmin(),
9 'Is route need permission admin'
10);
11
12report_unless(
13 Auth::user()->isAdmin(),
14 'Is route need permission user'
15);
These helpers are optional—you are free to use them or not 🙂
Custom mutex name of scheduled events
Andrej Mihaliak contributed a callback to resolve a custom mutex name of scheduled events:
1$schedule->command('do-something', ['--uuid' => 'b4d5f926-e1b5-4285-b068-cf1d623a0635'])
2 ->description('Do something important.')
3 ->everyFiveMinutes()
4 ->onOneServer()
5 ->withoutOverlapping()
6 ->createMutexNameUsing(fn (Event $event) => 'schedule-task-'.sha1($event->description));
Check out Pull Request #45126 for complete details.
Release Notes
You can see the complete list of new features and updates below and the diff between 9.41.0 and 9.42.0 on GitHub. The following release notes are directly from the changelog:
v9.42.0
Added
- Added –rest option to queue:listen (00a12e2, 82fde9e)
- Added
Illuminate/Support/Stringable::isUlid()
(#45100) - Add news report_if and report_unless helpers functions (#45093)
- Add callback to resolve custom mutex name of schedule events (#45126)
- Add WorkOptions to WorkerStopping Event (#45120)
- Added
singleton
andcreatable
options toIlluminate/Routing/Console/ControllerMakeCommand
(#44872)
Fixed
- Fix pure enums validation (#45121)
- Prevent test issues with relations with the $touches property (#45118)
- Fix factory breaking when trying to determine whether a relation is empty (#45135)
Changed