technote / laravel-transaction-fire-event
Controlling events that occur in a transaction
Fund package maintenance!
paypal.me/technote0space
Installs: 5 229
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 3
Requires
- php: ^7.4|^8.0
- laravel/framework: *
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.32.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.2
- fakerphp/faker: ^1.19
- orchestra/testbench: ^6.24
- phpmd/phpmd: ^2.12
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
README
Read this in other languages: English, 日本語.
Controlling events that occur in a transaction.
Table of Contents
Install
composer require technote/laravel-transaction-fire-event
Usage
-
In the model where you want to control the firing of events, use
DelayFireEvent
trait.<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Technote\TransactionFireEvent\Models\DelayFireEvent; class Item extends Model { use DelayFireEvent; public static function boot() { parent::boot(); self::saved(function ($model) { // }); } // relation example public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class); } }
-
If used within a transaction,
saved
anddeleted
events will be held until the end of the transaction.DB::transaction(function () { $item = new Item(); $item->name = 'test'; $item->save(); // The `saved` event will not be fired here yet. $item->tags()->sync([1, 2, 3]); } // The `saved` event is called at the end of the transaction, // so you can get the synchronized tags with `$model->tags()->sync`.
Change the event to hold fire
The target events are saved
and deleted
by default.
To change it, override getDelayTargetEvents
.
protected function getDelayTargetEvents(): array { return [ 'created', 'updated', 'saved', 'deleted', ]; }