承接 mvanduijker/laravel-transactional-model-events 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mvanduijker/laravel-transactional-model-events

最新稳定版本:3.0.1

Composer 安装命令:

composer require mvanduijker/laravel-transactional-model-events

包简介

Add eloquent model events fired after a transaction is committed or rolled back

README 文档

README

Latest Version on Packagist Build Status Total Downloads

Add transactional events to your eloquent models. Will automatically detect changes in your models within a transaction and will fire events on commit or rollback. Should mimic the same functionality as transactional callbacks in Ruby on Rails.

You want to use this if you want to listen on events fired by models within a transaction and you want to be sure the transaction has completed successfully (or is rolled back).

Installation

You can install the package via composer:

composer require mvanduijker/laravel-transactional-model-events

Usage

Just add the trait TransactionalAwareEvents to your model or base model.

<?php

class MyModel extends Model
{
    use TransactionalAwareEvents;
}

The following events will become available:

  • afterCommit.created
  • afterCommit.saved
  • afterCommit.updated
  • afterCommit.deleted
  • afterCommit.restored
  • afterCommit.forceDeleted
  • afterRollback.created
  • afterRollback.saved
  • afterRollback.updated
  • afterRollback.deleted
  • afterRollback.restored
  • afterRollback.forceDeleted

You can add listeners in you EventServiceProvider the same way as normal events

<?php

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'eloquent.afterCommit.created: App\Models\Shipment' => [
        'App\Listeners\SendShipmentNotification',
    ],
];

Or you can put them in your model boot method:

<?php

class PictureFile extends Model
{
    use TransactionalAwareEvents;
    
    public static function boot()
    {
        parent::boot();
        
        static::registerModelEvent('afterCommit.deleted', function ($model) {
            if (Storage::exists($model->file)) {
                Storage::delete($model->file);            
            }
        });
    }
}

You should also be able to map them to event classes

<?php

class PictureFile extends Model
{
    use TransactionalAwareEvents;
    
    protected $dispatchesEvents = [
        'afterCommit.created' => PictureFileCreated::class,
        'afterCommit.deleted' => PictureFileDeleted::class,
    ];
}

And as icing on the cake, you can observe them with the following methods:

  • afterCommitCreated
  • afterCommitSaved
  • afterCommitUpdated
  • afterCommitDeleted
  • afterCommitRestored
  • afterCommitForceDeleted
  • afterRollbackCreated
  • afterRollbackSaved
  • afterRollbackUpdated
  • afterRollbackDeleted
  • afterRollbackRestored
  • afterRollbackForceDeleted

For example:

<?php

class PictureFileObserver
{
    public function afterCommitDeleted(PictureFile $model)
    {
        if (Storage::exists($model->file)) {
            Storage::delete($model->file);            
        }
    }
}

And register the observer in you ServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        PictureFile::observe(PictureFileObserver::class);
    }
}

Since Laravel version 10.44 it's also possible to register the observer with theObservedBy attribute.

<?php

use Illuminate\Database\Eloquent\Attributes\ObservedBy; 

#[ObservedBy(PictureFileObserver::class)]
class PictureFileObserver
{
...
}

Multiple database connections are supported, events are triggered when the transaction is committed on the configured connection of the model.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 152.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 75
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 75
  • Watchers: 3
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-14