├── LICENSE.md ├── composer.json ├── CHANGELOG.md ├── .github └── workflows │ └── run-tests.yml ├── CONTRIBUTING.md ├── src └── TransactionalAwareEvents.php └── README.md /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Mark van Duijker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mvanduijker/laravel-transactional-model-events", 3 | "description": "Add eloquent model events fired after a transaction is committed or rolled back", 4 | "keywords": [ 5 | "laravel", 6 | "eloquent", 7 | "transaction", 8 | "database", 9 | "events", 10 | "laravel-transactional-model-events" 11 | ], 12 | "homepage": "https://github.com/mvanduijker/laravel-transactional-model-events", 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Mark van Duijker", 17 | "email": "mark@duyker.nl", 18 | "homepage": "https://github.com/mvanduijker", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": "^8.0", 24 | "illuminate/database": "~9.0|~10.0|~11.0|~12.0" 25 | }, 26 | "require-dev": { 27 | "larapack/dd": "^1.0", 28 | "orchestra/testbench": "~7.0|~8.0|~9.0|~10.0", 29 | "phpunit/phpunit": "^9.5|^10.0|^11.5" 30 | }, 31 | "minimum-stability": "dev", 32 | "prefer-stable": true, 33 | "autoload": { 34 | "psr-4": { 35 | "MVanDuijker\\TransactionalModelEvents\\": "src" 36 | } 37 | }, 38 | "autoload-dev": { 39 | "psr-4": { 40 | "MVanDuijker\\TransactionalModelEvents\\Tests\\": "tests" 41 | } 42 | }, 43 | "scripts": { 44 | "test": "vendor/bin/phpunit", 45 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 46 | }, 47 | "config": { 48 | "sort-packages": true 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-transactional-model-events` will be documented in this file 4 | 5 | ## 3.0.0 - 2025-11-08 6 | 7 | - Support php 8.5 8 | 9 | ## 3.0.0 - 2025-03-15 10 | 11 | - Add support for observing through annotations 12 | - possible BC issue if you have overridden Model::getObservableEvents 13 | 14 | ## 2.9.0 - 2025-02-26 15 | 16 | - Add support for Laravel 12 17 | 18 | ## 2.8.0 - 2024-03-13 19 | 20 | - Add support for Laravel 11 21 | - Added PHP 8.3 in the pipeline 22 | 23 | ## 2.7.0 - 2023-02-14 24 | 25 | - Add support for Laravel 10 26 | - Added PHP 8.2 in the pipeline 27 | 28 | ## 2.6.0 - 2022-02-09 29 | 30 | - support for laravel 9 31 | 32 | ## 2.5.0 - 2021-02-05 33 | 34 | - support for php 8 35 | 36 | ## 2.4.0 - 2020-09-08 37 | 38 | - support for Laravel 8 39 | - dropped support for php 7.2 40 | - dropped support for Laravel 5.x 41 | 42 | ## 2.3.1 - 2020-08-27 43 | 44 | - fix possible issue with Laravel Nova 45 | 46 | ## 2.3.0 - 2020-03-03 47 | 48 | - support for laravel 7.0 49 | 50 | ## 2.2.0 - 2020-02-21 51 | 52 | - Added support for multiple database connections 53 | 54 | ## 2.1.0 - 2020-01-07 55 | 56 | - Removed support for php 7.1 57 | - Added support for php 7.4 58 | - Fixed dependencies so it properly tests against latest laravel version 59 | - Ignore laravel version 6.9.0 which broke this package and is fixed from 6.10.0 [more information](https://github.com/laravel/framework/issues/30948) 60 | 61 | ## 2.0.0 - 2019-08-28 62 | 63 | - Add support for observers 64 | - removed support for laravel 5.5 and 5.6 65 | - Added support for laravel 6.0 66 | 67 | ## 1.0.1 - 2019-06-13 68 | 69 | - fix firing model events with nested transactions. Only when outer transaction is committed the model events are fired. 70 | 71 | ## 1.0.0 - 2019-05-19 72 | 73 | - initial release 74 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Run tests 2 | 3 | on: 4 | - push 5 | 6 | jobs: 7 | test: 8 | runs-on: ${{ matrix.os }} 9 | 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | os: [ubuntu-latest] 14 | php: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5] 15 | laravel: ["9.*", "10.*", "11.*", "12.*"] 16 | dependency-version: [prefer-stable] 17 | include: 18 | - testbench: 7.* 19 | laravel: 9.* 20 | - testbench: 8.* 21 | laravel: 10.* 22 | - testbench: 9.* 23 | laravel: 11.* 24 | - testbench: 10.* 25 | laravel: 12.* 26 | exclude: 27 | - php: 8.0 28 | laravel: 10.* 29 | - php: 8.0 30 | laravel: 11.* 31 | - php: 8.0 32 | laravel: 12.* 33 | - php: 8.1 34 | laravel: 11.* 35 | - php: 8.1 36 | laravel: 12.* 37 | 38 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 39 | 40 | steps: 41 | - name: Checkout code 42 | uses: actions/checkout@v4 43 | 44 | - name: Cache dependencies 45 | uses: actions/cache@v4 46 | with: 47 | path: ~/.composer/cache/files 48 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 49 | 50 | - name: Setup PHP 51 | uses: shivammathur/setup-php@v2 52 | with: 53 | php-version: ${{ matrix.php }} 54 | extensions: pdo, sqlite, pdo_sqlite 55 | coverage: pcov 56 | 57 | - name: Install dependencies 58 | run: | 59 | composer require "illuminate/database:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 60 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 61 | 62 | - name: Execute tests 63 | run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 64 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /src/TransactionalAwareEvents.php: -------------------------------------------------------------------------------- 1 | getConnection()->transactionLevel()) { 33 | // In some rare cases the connection name on the model can be null, 34 | // fallback on the connection name from the connection 35 | $connectionName = $model->getConnectionName() ?? $model->getConnection()->getName(); 36 | self::$queuedTransactionalEvents[$connectionName][$event][] = $model; 37 | } else { 38 | // auto fire the afterCommit callback when we are not in a transaction 39 | $model->fireModelEvent('afterCommit.' . $event); 40 | $model->fireModelEvent('afterCommit' . ucfirst($event)); 41 | } 42 | }); 43 | } 44 | 45 | $dispatcher->listen(TransactionCommitted::class, function (TransactionCommitted $event) { 46 | if ($event->connection->transactionLevel() > 0) { 47 | return; 48 | } 49 | 50 | foreach ((self::$queuedTransactionalEvents[$event->connectionName] ?? []) as $eventName => $models) { 51 | /** @var Model $model */ 52 | foreach ($models as $model) { 53 | $model->fireModelEvent('afterCommit.' . $eventName); 54 | $model->fireModelEvent('afterCommit' . ucfirst($eventName)); 55 | } 56 | } 57 | self::$queuedTransactionalEvents[$event->connectionName] = []; 58 | }); 59 | 60 | $dispatcher->listen(TransactionRolledBack::class, function (TransactionRolledBack $event) { 61 | if ($event->connection->transactionLevel() > 0) { 62 | return; 63 | } 64 | 65 | foreach ((self::$queuedTransactionalEvents[$event->connectionName] ?? []) as $eventName => $models) { 66 | /** @var Model $model */ 67 | foreach ($models as $model) { 68 | $model->fireModelEvent('afterRollback.' . $eventName); 69 | $model->fireModelEvent('afterRollback' . ucfirst($eventName)); 70 | } 71 | } 72 | self::$queuedTransactionalEvents[$event->connectionName] = []; 73 | }); 74 | } 75 | 76 | public function getObservableEvents() 77 | { 78 | $observableEvents = parent::getObservableEvents(); 79 | 80 | foreach (self::$transactionalEloquentEvents as $eloquentEvent) { 81 | $observableEvents[] = 'afterCommit' . ucfirst($eloquentEvent); 82 | $observableEvents[] = 'afterRollback' . ucfirst($eloquentEvent); 83 | 84 | } 85 | 86 | return $observableEvents; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Transactional Model Events 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/mvanduijker/laravel-transactional-model-events.svg?style=flat-square)](https://packagist.org/packages/mvanduijker/laravel-transactional-model-events) 4 | ![Build Status](https://github.com/mvanduijker/laravel-transactional-model-events/workflows/Run%20tests/badge.svg) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/mvanduijker/laravel-transactional-model-events.svg?style=flat-square)](https://packagist.org/packages/mvanduijker/laravel-transactional-model-events) 6 | 7 | 8 | Add transactional events to your eloquent models. Will automatically detect changes in your models within a transaction 9 | and will fire events on commit or rollback. Should mimic the same functionality as 10 | [transactional callbacks](https://guides.rubyonrails.org/active_record_callbacks.html#transaction-callbacks) in Ruby on 11 | Rails. 12 | 13 | You want to use this if you want to listen on events fired by models within a transaction and you want to be sure the transaction has completed successfully (or is rolled back). 14 | 15 | 16 | ## Installation 17 | 18 | You can install the package via composer: 19 | 20 | ```bash 21 | composer require mvanduijker/laravel-transactional-model-events 22 | ``` 23 | 24 | ## Usage 25 | 26 | Just add the trait TransactionalAwareEvents to your model or base model. 27 | 28 | ```php 29 | [ 64 | 'App\Listeners\SendShipmentNotification', 65 | ], 66 | ]; 67 | 68 | ``` 69 | 70 | Or you can put them in your model boot method: 71 | 72 | ```php 73 | file)) { 85 | Storage::delete($model->file); 86 | } 87 | }); 88 | } 89 | } 90 | ``` 91 | 92 | You should also be able to map them to event classes 93 | 94 | ```php 95 | PictureFileCreated::class, 103 | 'afterCommit.deleted' => PictureFileDeleted::class, 104 | ]; 105 | } 106 | ``` 107 | 108 | And as icing on the cake, you can observe them with the following methods: 109 | 110 | * `afterCommitCreated` 111 | * `afterCommitSaved` 112 | * `afterCommitUpdated` 113 | * `afterCommitDeleted` 114 | * `afterCommitRestored` 115 | * `afterCommitForceDeleted` 116 | * `afterRollbackCreated` 117 | * `afterRollbackSaved` 118 | * `afterRollbackUpdated` 119 | * `afterRollbackDeleted` 120 | * `afterRollbackRestored` 121 | * `afterRollbackForceDeleted` 122 | 123 | For example: 124 | 125 | ```php 126 | file)) { 133 | Storage::delete($model->file); 134 | } 135 | } 136 | } 137 | ``` 138 | 139 | And register the observer in you ServiceProvider: 140 | 141 | ```php 142 |