PHP code example of technote / laravel-transaction-fire-event

1. Go to this page and download the library: Download technote/laravel-transaction-fire-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

technote / laravel-transaction-fire-event example snippets


   
   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);
       }
   }
   

   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`.
   

protected function getDelayTargetEvents(): array
{
    return [
        'created',
        'updated',
        'saved',
        'deleted',
    ];
}