News

Queue totalSize() and JobInterrupted Event in Laravel 13.31

Published
Queue totalSize() and JobInterrupted Event in Laravel 13.31 image

The Laravel team released v13.31.0 with a queue connection totalSize() method, a new JobInterrupted event, chaperone support for BelongsToMany pivot models, and more.

Queue Total Size

Jack Bayliss contributed the totalSize() method, which answers the question "how many total jobs do I have on my queue connection right now?"

 
// Before
Queue::totalPendingSize() + Queue::totalDelayedSize() + Queue::totalReservedSize();
 
// After
Queue::totalSize();

See #61373 for more details.

Job Interrupted Event

Jack Bayliss contributed a new JobInterrupted event that is fired when a job holding the Interruptible contract is interrupted by the worker. This allows you to listen for this event and perform any necessary cleanup or logging.

use Illuminate\Queue\Events\JobInterrupted;
 
Event::listen(JobInterrupted::class, function (JobInterrupted $event) {
// Handle the job interruption
});

See #61412 for more details.

Chaperone Support for BelongsToMany Pivot Models

Caleb White contributed chaperone support for BelongsToMany pivot models. This allows you to use the chaperone() method on a BelongsToMany relationship to retrieve the pivot model instance.

Many times when using custom pivot models, the BelongsTo relations can be defined on the model---however, trying to use them requires loading the models from the database even though both of the models are available during the BelongsToMany hydration.

...This PR adds chaperone support to the BelongsToMany relation, if a custom pivot model is not provided then calling ->chaperone() is a no-op.

class PostTagPivot extends Pivot
{
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
 
public function tag(): BelongsTo
{
return $this->belongsTo(Tag::class);
}
}
 
class Tag extends Model
{
public function posts(): BelongsToMany
{
return $this->belongsToMany(Post::class)
->using(PostTagPivot::class)
// automatically guesses relationship names
->chaperone();
// but can override if needed or non-standard
->chaperone(declaring: 'tag', related: 'post');
}
}
 
class Post extends Model
{
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class)
->using(PostTagPivot::class)
->chaperone(declaring: 'post', related: 'tag');
}
}

See #61152 for more details.

Vite Dev Server URL

@ramonmalcolm10 contributed a devServerUrl() method to the Vite class, which allows you to retrieve the URL of the Vite development server. This is useful for generating asset URLs in your application.

See #61465 for more details.

Once Assertions for Mail and Notification Fakes

@talaridisTh contributed new "once" assertions for the Mail and Notification fakes:

// Before
Mail::assertSentTimes(OrderShipped::class, 1);
Mail::assertQueuedTimes(OrderShipped::class, 1);
 
Notification::assertSentToTimes($user, InvoicePaid::class, 1);
Notification::assertSentOnDemandTimes(InvoicePaid::class, 1);
 
// After
Mail::assertSentOnce(OrderShipped::class);
Mail::assertQueuedOnce(OrderShipped::class);
 
Notification::assertSentToOnce($user, InvoicePaid::class);
Notification::assertSentOnDemandOnce(InvoicePaid::class);

See #61415 for more details.

Release Notes

You can see the complete list of new features and updates below and the diff between 13.30.0 and 13.31.0 on GitHub. The following release notes are directly from the changelog:

v13.31.0

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Find Unexpected Test Inputs with Fuzz for Pest image

Find Unexpected Test Inputs with Fuzz for Pest

Read article
Laravel Rulebook: Business Rules That Change by Date image

Laravel Rulebook: Business Rules That Change by Date

Read article
Taylor disabled GitHub Issues on most Laravel open-source packages. image

Taylor disabled GitHub Issues on most Laravel open-source packages.

Read article
Exclude Vendor and Default Commands in `php artisan dev` image

Exclude Vendor and Default Commands in `php artisan dev`

Read article
The Laracon Archive image

The Laracon Archive

Read article
Group Adjacent Collection Items in Laravel with chunkBy() image

Group Adjacent Collection Items in Laravel with chunkBy()

Read article