technote/laravel-transaction-fire-event
Laravel Event Control Library
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
DelayFireEventtrait.<?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,
savedanddeletedevents 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',
];
}
Author
Related Packages
ytake/laravel-aspect
Aspect Oriented Programming library for laravel framework, and lumen
143,928
138
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v0.3.6 | ^7.4| | * | MIT | |
| v0.3.5 | ^7.4| | * | MIT | |
| v0.3.4 | ^7.4| | * | MIT | |
| v0.3.3 | ^7.4| | * | MIT | |
| v0.3.2 | ^7.4| | * | MIT | |
| v0.3.1 | ^7.4| | * | MIT | |
| v0.3.0 | ^7.4| | * | MIT | |
| v0.2.2 | ^7.4| | * | MIT | |
| v0.2.1 | ^7.4| | * | MIT | |
| v0.2.0 | ^7.4| | * | MIT | |
| v0.1.0 | ^7.4| | * | MIT | |
| v0.0.1 | ^7.4| | * | MIT |