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