Never Miss a Laravel Release π
The Laravel team released version 12.32.0 this week with a new HTTP batch method, an "after" rate limiting method to control rate limiting by response, batch job failure callback support, and more.
HTTP Batch Requests
Wendell Adriel contributed an Http::batch() method to give you a way of working with concurrent requests in a batch, and allows you to define various callbacks for the batch's lifecycle:
use Illuminate\Http\Client\Batch;use Illuminate\Http\Client\RequestException;use Illuminate\Http\Client\Response;use Illuminate\Support\Facades\Http; $responses = Http::batch(fn (Batch $batch) => [ $batch->get('http://localhost/first'), $batch->get('http://localhost/second'), $batch->get('http://localhost/third'),])->before(function (Batch $batch) { // The batch has been created but no requests have been initialized...})->progress(function (Batch $batch, int|string $key, Response $response) { // An individual request has completed successfully...})->then(function (Batch $batch, array $results) { // All requests completed successfully...})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) { // First batch request failure detected...})->finally(function (Batch $batch, array $results) { // The batch has finished executing...})->send();
You can also name your requests using the as() method as follows:
$responses = Http::batch(fn (Batch $batch) => [ $batch->as('first')->get('http://localhost/first'), $batch->as('second')->get('http://localhost/second'), $batch->as('third')->get('http://localhost/third'),])->send();
Finally, the Http batch has various properties and methods you can use to inspect the batch:
// The number of requests assigned to the batch...$batch->totalRequests; // The number of requests that have not been processed yet...$batch->pendingRequests; // The number of requests that have failed...$batch->failedRequests; // The number of requests that have been processed thus far...$batch->processedRequests(); $batch->finished(); // Bool$batch->hasFailures(); // Bool
You can see the implementation in Pull Request #56946. The request batching documentation provides usage details.
Database afterRollback() Callback Support
Malte Kuhr contributed an afterRollback() method to respond to database transactions that are rolled back:
use Illuminate\Support\Facades\DB; DB::transaction(function () { DB::afterCommit(function () { // ... }); DB::afterRollBack(function () { // ... }); // Run code in the transaction... // If the transaction fails, the `afterRollback()` callback will run.});
Batch Job Failure Callback Support
Yitz Willroth updated PendingBatch::allowFailures() to allow callbacks or an array of callbacks. Before this PR, allowFailures() was a way to indicate that batches could disable the default behavior of being cancelled when a failure happens. If you pass a callback (new behavior), it will run on each failure:
// New usage example:$batch->allowFailures(function ($batch, $exception) { // Handle individual job failure Log::error("Job failed in batch {$batch->id}: {$exception->getMessage()}");});
See Pull Request #55916 for implementation details and discussion.
Introduce "after" Rate Limiting
Tim MacDonald contributed an after() method you can use to rate limit based on the response. He had this to say about it in the pull request:
Currently, it is only possible to rate limit based on the request. Often, it can be useful to rate limit based on the response.
From the original PR: For example, imagine you have a sign up endpoint that could return validation errors. If you want to throttle to a single sign up per day for an IP address, any validation response would trigger the rate limiter making it impossible to sign up again for the day.
Another example is rate limiting 404 responses to mitigate enumeration attacks on resource identifiers. If a single user hits a certain level of 404 responses in a short time frame, they could be attempting enumeration attacks.
Here's the example from the same pull request description of how this callback might be used:
use Illuminate\Http\Request;use Illuminate\Support\Facades\RateLimiter;use Symfony\Component\HttpFoundation\Response; /* * Ensure a user can only hit ten 404 responses in a minute before they are * rate limited to ensure users cannot enumerate resource IDs. */RateLimiter::for('resource-not-found', function (Request $request) { return Limit::perMinute(10) ->by("user:{$request->user()->id}") // The new `after` hook... ->after(function (Response $response) { return $response->getStatusCode() === 404; });});
Release notes
You can see the complete list of new features and updates below and the diff between 12.31.0 and 12.32.0 on GitHub. The following release notes are directly from the changelog:
v12.32.0
- [12.x] fix static analysis error by @cosmastech in https://github.com/laravel/framework/pull/57162
- Fix: Handle non-string returns from Htmlable::toHtml() in e() helper by @Carnicero90 in https://github.com/laravel/framework/pull/57157
- [12.x] Fix pending attributes in schedule group by @jamessa in https://github.com/laravel/framework/pull/57156
- Remove Request overview from Exceptions by @barryvdh in https://github.com/laravel/framework/pull/57158
- [12.x] Pass "throw" option from scoped to parent disk by @daniser in https://github.com/laravel/framework/pull/57163
- [12.x] Make docblock return type in line with actual return type by @parijke in https://github.com/laravel/framework/pull/57164
- [12.x] Adjust
Arrtypehints by @daniser in https://github.com/laravel/framework/pull/57165 - [12.x] Track filesystem adapter decoration by @daniser in https://github.com/laravel/framework/pull/57167
- [12.x] Batch Job Failure Callbacks Support by @yitzwillroth in https://github.com/laravel/framework/pull/55916
- [12.x] Fix operator precedence by @daniser in https://github.com/laravel/framework/pull/57169
- [12.x] Clean up after filesystem manager tests by @daniser in https://github.com/laravel/framework/pull/57168
- Fix: Improve validateInteger ergonomics and fix BC break by @ntm-dev in https://github.com/laravel/framework/pull/57175
- [12.x] Fix nested
canand inherit models on route groups by @bonroyage in https://github.com/laravel/framework/pull/57172 - [12.x] Syntax highlight on the frontend by @avosalmon in https://github.com/laravel/framework/pull/57184
- [12.x] Add missing Closure type to Collection::pluck() docblock by @Bariss61 in https://github.com/laravel/framework/pull/57178
- Add database afterRollback callback support and tests by @maltekuhr in https://github.com/laravel/framework/pull/57180
- fix: add return type by @alipowerful7 in https://github.com/laravel/framework/pull/57192
- [12.x] Adds support enums for
ThrottleRequests::usingmethod by @sethsandaru in https://github.com/laravel/framework/pull/57190 - [12.x] Introduce "after" rate limiting by @timacdonald in https://github.com/laravel/framework/pull/57125
- [12.x] Json schema nullable by @Katalam in https://github.com/laravel/framework/pull/57181
- [12.x] Dispatch framework events on composer
pre-package-uninstallevent by @cosmastech in https://github.com/laravel/framework/pull/57144 - [12.x] Add Http::batch by @WendellAdriel in https://github.com/laravel/framework/pull/56946
- [12.x] [Mail] Update
queuePHPDoc according to function behavior by @MrYamous in https://github.com/laravel/framework/pull/57207 - [12.x] Remove unnecessary parentheses by @AhmedAlaa4611 in https://github.com/laravel/framework/pull/57212
- [12.x] Remove unnecessary parentheses by @AhmedAlaa4611 in https://github.com/laravel/framework/pull/57210
- [12.x] Fixes error renderer report page by @xiCO2k in https://github.com/laravel/framework/pull/57208
- [12.x] Extend SQS FIFO and fair queue support by @patrickcarlohickman in https://github.com/laravel/framework/pull/57187