🎉🎉 Larasense is officially launched 🎉🎉
- Your Hub for Laravel News, Trends & Updates

Deferring Events Including Model Events in Laravel

laravel
tutorial
events
Nabil Hassen
Nabil Hassen
Aug 18, 2025
Deferring Events in Laravel
Last updated on Aug 18, 2025
Table of contents:

Deferring Events in Laravel

Laravel provides an elegant way to handle events in your applications. But what if you want to delay the dispatching of certain events until after a block of code has finished executing? That’s where deferred events come in.

In this post, we’ll explore what deferred events are, why they’re useful, and how you can implement them in your Laravel applications.

What Are Deferred Events in Laravel?

Deferred events allow you to delay the dispatching of model events and execution of event listeners until after a specific closure has finished running. This feature is particularly useful when you need to ensure that all related database records are created and available before event listeners are triggered.

For example, consider a scenario where you create an Order and immediately create OrderLineItems linked to that order. If events were dispatched immediately, your event listeners might not yet have access to all the related records.

How to Use Event::defer()

Laravel makes deferring events simple with the Event::defer() method. You just wrap your logic inside a closure:

use App\Models\Order;
use Illuminate\Support\Facades\Event;
 
Event::defer(function () {
$order = Order::create(['order_number' => 'ORD-1001']);
 
$order->lineItems()->create([
'product_name' => 'Laptop',
'quantity' => 1,
]);
});

What happens here?

  • All events triggered inside the closure will be queued up.
  • They are only dispatched after the closure completes.
  • If an exception occurs inside the closure, no events will be dispatched.

This ensures event listeners run in a clean, predictable state where all related records already exist.

Deferring Specific Events Only

Sometimes you may not want to defer all events, only specific ones. Laravel allows you to do this by passing an array of event names as the second parameter to defer().

Example:

use App\Models\Order;
use Illuminate\Support\Facades\Event;
 
Event::defer(function () {
$order = Order::create(['order_number' => 'ORD-1001']);
 
$order->lineItems()->create([
'product_name' => 'Laptop',
'quantity' => 1,
]);
}, ['eloquent.created: '.Order::class]);

Explanation:

  • In this case, only the eloquent.created event for the Order model will be deferred.
  • Other events (like those for the OrderLineItem model) will be dispatched immediately.

When to Use Deferred Events

You might want to defer events when:

  • Creating related records where listeners depend on all records being available.
  • Running bulk insert operations that should only trigger events after completion.
  • Avoiding inconsistent state issues in event listeners.

Conclusion

Deferred events in Laravel provide a powerful way to manage when your events are fired. By using Event::defer(), you can ensure that listeners always have access to the final, complete state of your data.

Whether you defer all events in a block of code or only specific ones, this feature can help you build more reliable and predictable event-driven applications in Laravel.

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts