New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

technote/laravel-transaction-fire-event

Controlling events that occur in a transaction
11,584 1
Install
composer require technote/laravel-transaction-fire-event
Latest Version:v0.3.6
PHP:^7.4|^8.0
Laravel:4 – 9
License:MIT
Last Updated:Sep 15, 2022
Links: GitHub  ·  Packagist
Maintainer: technote

Laravel Event Control Library

CI Status codecov CodeFactor License: MIT PHP: >=7.4

Read this in other languages: English, 日本語.

Controlling events that occur in a transaction.

Packagist

Table of Contents

Details

Install

composer require technote/laravel-transaction-fire-event

Usage

  1. 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);
        }
    }
    
  2. If used within a transaction, saved and deleted 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',
    ];
}

Author

GitHub (Technote)
Blog

Related Packages

deferdie/event

Event management for Laravel 5

16 0
humweb/event

This package helps to create event dispatcher services

46 0
envant/fireable

An elegant way to trigger events based on attributes changes

803,948 442
ilzrv/fireable

An elegant way to trigger events based on attributes changes

9,168 0
desmart/laravel-event-sourcing

Laravel bindings for DeSmart Event Sourcing package

37 7

Version History

Version Released PHP Laravel License
v0.3.6 ^7.4|^8.0 * MIT
v0.3.5 ^7.4|^8.0 * MIT
v0.3.4 ^7.4|^8.0 * MIT
v0.3.3 ^7.4|^8.0 * MIT
v0.3.2 ^7.4|^8.0 * MIT