PHPackages                             ttlove/laravel-transactional-events - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ttlove/laravel-transactional-events

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ttlove/laravel-transactional-events
===================================

Transaction-aware Event Dispatcher for Laravel

1.8.9(6y ago)08MITPHP

Since Sep 6Pushed 5y agoCompare

[ Source](https://github.com/TTLOVE/laravel-transactional-events)[ Packagist](https://packagist.org/packages/ttlove/laravel-transactional-events)[ RSS](/packages/ttlove-laravel-transactional-events/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (31)Used By (0)

Transaction-aware Event Dispatcher for Laravel
==============================================

[](#transaction-aware-event-dispatcher-for-laravel)

[![Latest Stable Version](https://camo.githubusercontent.com/bfdba2098b1a0d567d10fb9b342076ca3372ed76b599645051433ea5571ce449/68747470733a2f2f706f7365722e707567782e6f72672f666e746e657665732f6c61726176656c2d7472616e73616374696f6e616c2d6576656e74732f762f737461626c65)](https://packagist.org/packages/fntneves/laravel-transactional-events)[![TravisCI Status](https://camo.githubusercontent.com/1b3b13e146f4bc5c82a105f7e4aa959e523a1dd5fa84ab9d11c4858270e24760/68747470733a2f2f7472617669732d63692e6f72672f666e746e657665732f6c61726176656c2d7472616e73616374696f6e616c2d6576656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fntneves/laravel-transactional-events)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9898e5c4c7780b4ac66005e1b7149786686c5fed9c28fcdb647e87ed03a3ec8c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f666e746e657665732f6c61726176656c2d7472616e73616374696f6e616c2d6576656e74732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/fntneves/laravel-transactional-events/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/e570c7469914e7137cbca503bb705730ec6a6bdf181663883381d78e270e245d/68747470733a2f2f706f7365722e707567782e6f72672f666e746e657665732f6c61726176656c2d7472616e73616374696f6e616c2d6576656e74732f646f776e6c6f616473)](https://packagist.org/packages/fntneves/laravel-transactional-events)

This Laravel package introduces Transaction-aware Event Dispatcher.
It ensures the events dispatched within a database transaction are dispatched only if the outer transaction successfully commits. Otherwise, the events are discarded and never dispatched.

Table of Contents
-----------------

[](#table-of-contents)

- [Motivation](#motivation)
- [Installation](#installation)
    - [Laravel](#laravel)
    - [Lumen](#lumen)
- [Usage](#usage)
- [Configuration](#configuration)
- [F.A.Q.](#frequently-asked-questions)
- [Known Issues](#known-issues)

Motivation
----------

[](#motivation)

Consider the following example of ordering tickets that involves changes to the database.
The `orderTickets` dispatches the custom `OrderCreated` event. In turn, its listener sends an email to the user with the order details.

```
DB::transaction(function() {
    ...
    $order = $concert->orderTickets($user, 3); // internally dispatches 'OrderCreated' event
    PaymentService::registerOrder($order);
});
```

In the case of transaction failure, due to an exception in the `orderTickets` method or even a deadlock, the database changes are completely discarded.

Unfortunately, this is not true for the already dispatched `OrderCreated` event. This results in sending the order confirmation email to the user, even after the order failure.

The purpose of this package is thus to hold events dispatched within a database transaction until it successfully commits. In the above example the `OrderCreated` event would never be dispatched in the case of transaction failure.

Installation
------------

[](#installation)

LaravelPackage5.5.x-5.7.x1.4.x5.8.x-7.x1.8.x### Laravel

[](#laravel)

- Install this package via `composer`:

```
composer require fntneves/laravel-transactional-events

```

- Publish the provided `transactional-events.php` configuration file:

```
php artisan vendor:publish --provider="Neves\Events\EventServiceProvider"

```

### Lumen

[](#lumen)

- Install this package via `composer`:

```
composer require fntneves/laravel-transactional-events
```

- Manually copy the provided `transactional-events.php` configuration file to the `config` folder:

```
cp vendor/fntneves/laravel-transactional-events/src/config/transactional-events.php config/transactional-events.php
```

- Register the configuration file and the service provider in `bootstrap/app.php`:

```
// Ensure the original EventServiceProvider is registered first, otherwise your event listeners are overriden.
$app->register(App\Providers\EventServiceProvider::class);

$app->configure('transactional-events');
$app->register(Neves\Events\EventServiceProvider::class);
```

Usage
-----

[](#usage)

The transaction-aware layer is enabled out of the box for the events under the `App\Events` namespace.

This package offers three distinct ways to dispatch transaction-aware events:

- Implement the `Neves\Events\Contracts\TransactionalEvent` contract;
- Use the generic `TransactionalClosureEvent` event;
- Change the [configuration file](#configuration).

#### Use the contract, Luke:

[](#use-the-contract-luke)

The simplest way to mark events as transaction-aware events is implementing the `Neves\Events\Contracts\TransactionalEvent` contract:

```
namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
...
use Neves\Events\Contracts\TransactionalEvent;

class TicketsOrdered implements TransactionalEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    ...
}
```

And that's it. There are no further changes required.

#### What about Jobs?

[](#what-about-jobs)

This package provides a generic `TransactionalClosureEvent` event for bringing the transaction-aware behavior to custom behavior without requiring specific events.

One relevant use case is to ensure that Jobs are dispatched only after the transaction successfully commits:

```
DB::transaction(function () {
    ...
    Event::dispatch(new TransactionalClosureEvent(function () {
        // Job will be dispatched only if the transaction commits.
        ProcessOrderShippingJob::dispatch($order);
    });
    ...
});
```

Configuration
-------------

[](#configuration)

The configuration file includes the following parameters:

Enable or disable the transaction-aware behavior:

```
'enable' => true
```

By default, the transaction-aware behavior will be applied to all events under the `App\Events` namespace.
Feel free to use patterns and namespaces.

```
'transactional' => [
    'App\Events'
]
```

Choose the events that should always bypass the transaction-aware layer, i.e., should be handled by the original event dispatcher. By default, all `*ed` Eloquent events are excluded. The main reason for this default value is to avoid interference with your already existing event listeners for Eloquent events.

```
'excluded' => [
    // 'eloquent.*',
    'eloquent.booted',
    'eloquent.retrieved',
    'eloquent.saved',
    'eloquent.updated',
    'eloquent.created',
    'eloquent.deleted',
    'eloquent.restored',
],
```

Frequently Asked Questions
--------------------------

[](#frequently-asked-questions)

#### Can I use it for Jobs?

[](#can-i-use-it-for-jobs)

Yes. As mentioned in [Usage](#usage), you can use the generic `TransactionalClosureEvent(Closure $callable)` event to trigger jobs only after the transaction commits.

Known issues
------------

[](#known-issues)

#### Transaction-aware events are not dispatched in tests.

[](#transaction-aware-events-are-not-dispatched-in-tests)

**This issue is fixed for Laravel 5.6.16+ (see [\#23832](https://github.com/laravel/framework/pull/23832)).**For previous versions, it is associated with the `RefreshDatabase` or `DatabaseTransactions` trait, namely when it uses database transactions to reset database after each test. This package relies on events dispached when transactions begin/commit/rollback and as each test is executed within a transaction that is rolled back when test finishes, the dispatched application events are never actually dispatched. In order to get the expected behavior, use the `Neves\Testing\RefreshDatabase` or `Neves\Testing\DatabaseTransactions` trait in your tests instead of the ones originally provided by Laravel.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 86.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~35 days

Recently: every ~12 days

Total

28

Last Release

2219d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/714535?v=4)[yazhouzhang](/maintainers/zyz)[@zyz](https://github.com/zyz)

---

Top Contributors

[![fntneves](https://avatars.githubusercontent.com/u/1722352?v=4)](https://github.com/fntneves "fntneves (97 commits)")[![mfn](https://avatars.githubusercontent.com/u/87493?v=4)](https://github.com/mfn "mfn (4 commits)")[![lokielse](https://avatars.githubusercontent.com/u/1573211?v=4)](https://github.com/lokielse "lokielse (2 commits)")[![erikgaal](https://avatars.githubusercontent.com/u/1234268?v=4)](https://github.com/erikgaal "erikgaal (1 commits)")[![Hanson](https://avatars.githubusercontent.com/u/10583423?v=4)](https://github.com/Hanson "Hanson (1 commits)")[![rtroost](https://avatars.githubusercontent.com/u/253343?v=4)](https://github.com/rtroost "rtroost (1 commits)")[![Szasza](https://avatars.githubusercontent.com/u/911466?v=4)](https://github.com/Szasza "Szasza (1 commits)")[![TTLOVE](https://avatars.githubusercontent.com/u/8059736?v=4)](https://github.com/TTLOVE "TTLOVE (1 commits)")[![adduc](https://avatars.githubusercontent.com/u/44527?v=4)](https://github.com/adduc "adduc (1 commits)")[![woodspire](https://avatars.githubusercontent.com/u/1275609?v=4)](https://github.com/woodspire "woodspire (1 commits)")[![digistorm-developer](https://avatars.githubusercontent.com/u/1697554?v=4)](https://github.com/digistorm-developer "digistorm-developer (1 commits)")[![duxthefux](https://avatars.githubusercontent.com/u/6758162?v=4)](https://github.com/duxthefux "duxthefux (1 commits)")

### Embed Badge

![Health badge](/badges/ttlove-laravel-transactional-events/health.svg)

```
[![Health](https://phpackages.com/badges/ttlove-laravel-transactional-events/health.svg)](https://phpackages.com/packages/ttlove-laravel-transactional-events)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[kirschbaum-development/commentions

A package to allow you to create comments, tag users and more

12369.2k](/packages/kirschbaum-development-commentions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
