https://github.com/technote-space/laravel-transaction-fire-event
https://github.com/technote-space/laravel-transaction-fire-event
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/technote-space/laravel-transaction-fire-event
- Owner: technote-space
- License: mit
- Created: 2021-08-08T11:28:07.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-15T16:46:42.000Z (almost 3 years ago)
- Last Synced: 2025-04-04T14:54:54.047Z (3 months ago)
- Language: PHP
- Size: 443 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.ja.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Laravel Event Control Library
[](https://github.com/technote-space/laravel-transaction-fire-event/actions)
[](https://codecov.io/gh/technote-space/laravel-transaction-fire-event)
[](https://www.codefactor.io/repository/github/technote-space/laravel-transaction-fire-event)
[](https://github.com/technote-space/laravel-transaction-fire-event/blob/main/LICENSE)
[](http://php.net/)*Read this in other languages: [English](README.md), [日本語](README.ja.md).*
トランザクション内で発生したイベントを制御するLaravelライブラリ
[Packagist](https://packagist.org/packages/technote/laravel-transaction-fire-event)
## Table of Contents
Details
- [インストール](#%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB)
- [使用方法](#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95)
- [発火を保留するイベントを変更](#%E7%99%BA%E7%81%AB%E3%82%92%E4%BF%9D%E7%95%99%E3%81%99%E3%82%8B%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%82%92%E5%A4%89%E6%9B%B4)
- [動機](#%E5%8B%95%E6%A9%9F)
- [Author](#author)## インストール
```
composer require technote/laravel-transaction-fire-event
```## 使用方法
1. イベントの発火を制御したいモデルで `DelayFireEvent` トレイトを使用```php
belongsToMany(Tag::class);
}
}
```2. トランザクション内で使用した場合、トランザクション終了時まで `saved`, `deleted` イベントの発火が保留される
```php
DB::transaction(function () {
$item = new Item();
$item->name = 'test';
$item->save();
// saved イベントはまだ発火されない
$item->tags()->sync([1, 2, 3]);
}// トランザクション終了時に saved イベントが呼ばれるため
// $model->tags()->sync で同期した tags が取得できる
```### 発火を保留するイベントを変更
対象のイベントはデフォルトで `saved`, `deleted` です。
変更するには `getDelayTargetEvents` をオーバーライドしてください。```php
protected function getDelayTargetEvents(): array
{
return [
'created',
'updated',
'saved',
'deleted',
];
}
```## 動機
saved イベントで他のサービスとデータを連携する際に、以下のような実装の場合にうまく行かなかったため。
```php
function save(Article $article, $entity) {
$article->title = $entity->getTitle();
// ...
$article->save(); // saved イベント発火(まだ tags は同期されていない)
$article->tags()->sync($entity->getTags());
}function create($entity) {
save(new Article(), $entity);
}function update($entity) {
save(Article::findOrFail($entity->getId()), $entity);
}
```https://github.com/fntneves/laravel-transactional-events
がやりたいことに近かったが、 Model で trait を use するだけでやりたかった。
## Author
[GitHub (Technote)](https://github.com/technote-space)
[Blog](https://technote.space)