Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mvanduijker/laravel-transactional-model-events
Add eloquent model events fired after a transaction is committed or rolled back
https://github.com/mvanduijker/laravel-transactional-model-events
database eloquent eloquent-models laravel php transaction
Last synced: 7 days ago
JSON representation
Add eloquent model events fired after a transaction is committed or rolled back
- Host: GitHub
- URL: https://github.com/mvanduijker/laravel-transactional-model-events
- Owner: mvanduijker
- License: mit
- Created: 2019-02-13T23:05:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-13T20:20:20.000Z (10 months ago)
- Last Synced: 2024-03-14T21:09:22.273Z (10 months ago)
- Topics: database, eloquent, eloquent-models, laravel, php, transaction
- Language: PHP
- Homepage:
- Size: 66.4 KB
- Stars: 66
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Transactional Model Events
[![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)
![Build Status](https://github.com/mvanduijker/laravel-transactional-model-events/workflows/Run%20tests/badge.svg)
[![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)Add transactional events to your eloquent models. Will automatically detect changes in your models within a transaction
and will fire events on commit or rollback. Should mimic the same functionality as
[transactional callbacks](https://guides.rubyonrails.org/active_record_callbacks.html#transaction-callbacks) in Ruby on
Rails.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).
## Installation
You can install the package via composer:
```bash
composer require mvanduijker/laravel-transactional-model-events
```## Usage
Just add the trait TransactionalAwareEvents to your model or base model.
```php
[
'App\Listeners\SendShipmentNotification',
],
];```
Or you can put them in your model boot method:
```php
file)) {
Storage::delete($model->file);
}
});
}
}
```You should also be able to map them to event classes
```php
PictureFileCreated::class,
'afterCommit.deleted' => PictureFileDeleted::class,
];
}
```And as icing on the cake, you can observe them with the following methods:
* `afterCommitCreated`
* `afterCommitSaved`
* `afterCommitUpdated`
* `afterCommitDeleted`
* `afterCommitRestored`
* `afterCommitForceDeleted`
* `afterRollbackCreated`
* `afterRollbackSaved`
* `afterRollbackUpdated`
* `afterRollbackDeleted`
* `afterRollbackRestored`
* `afterRollbackForceDeleted`For example:
```php
file)) {
Storage::delete($model->file);
}
}
}
```And register the observer in you ServiceProvider:
```php