Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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