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.

Job Middleware for Preventing Overlapping Jobs

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)
    ];
}

Rate Limit Jobs

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.

Preserve Eloquent Collection Type after Calling fresh()

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

Laravel 8.11 Released
1.70 GEEK