The Laravel team released 8.11 this week with two new job middleware, preserving eloquent collection type when calling fresh, and the latest new features, fixes, and changes in the 8.x branch.
Paras Malhotra contributed a new job middleware for preventing overlapping jobs. An example use-case could be stopping a double refund for an order:
use Illuminate\Queue\Middleware\WithoutOverlapping;
public function middleware()
{
return [
new WithoutOverlapping($this->order->id)
];
}
Paras Malhotra also contributed a RateLimitsJobs
and RateLimitsJobsWithRedis
middleware to the queuing system. It uses the same rate limiter class introduced in Laravel 8:
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('backups', function ($job) {
return $job->user->vipCustomer()
? Limit::none()
: Limit::perHour(1)->by($job->user->id);
});
Next, you can reference the backups rate limiter in the job middleware definition:
use Illuminate\Queue\Middleware\RateLimitsJobs;
public function middleware()
{
return [new RateLimitsJobs('backups')];
}
The queue middleware documentation includes the middleware introduced in this release.
Caleb Porzio contributed an update to preserve eloquent collection type after calling ->fresh()
. The pull request has the following example to illustrate the change:
// Current behavior
$users = User::all();
$users->fresh(); // Returns instance of "Illuminate\Database\Eloquent\Collection"
$users = User::all()
$users->first()->delete();
$users->fresh(); // Returns instance of "Illuminate\Support\Collection"
// New behavior
$users = User::all();
$users->fresh(); // Returns instance of "Illuminate\Database\Eloquent\Collection"
$users = User::all()
$users->first()->delete();
$users->fresh(); // // Returns instance of "Illuminate\Database\Eloquent\Collection" as well
#news #laravel 8