All Projects → mvanduijker → Laravel Transactional Model Events

mvanduijker / Laravel Transactional Model Events

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

Projects that are alternatives of or similar to Laravel Transactional Model Events

Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+357.69%)
Mutual labels:  eloquent, eloquent-models, database
Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (+857.69%)
Mutual labels:  eloquent, eloquent-models
Wp Eloquent
Eloquent ORM for WordPress
Stars: ✭ 478 (+819.23%)
Mutual labels:  eloquent, database
Laravel
Laravel Model Generator
Stars: ✭ 715 (+1275%)
Mutual labels:  eloquent, database
laravel-autonumber
Laravel package to create autonumber for Eloquent model
Stars: ✭ 26 (-50%)
Mutual labels:  eloquent, eloquent-models
Dbreeze
C# .NET MONO NOSQL ( key value store embedded ) ACID multi-paradigm database management system.
Stars: ✭ 383 (+636.54%)
Mutual labels:  transaction, database
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+1100%)
Mutual labels:  eloquent, database
laravel-nestedupdater
Package for allowing updating of nested Eloquent model relations using a single nested data array.
Stars: ✭ 19 (-63.46%)
Mutual labels:  eloquent, eloquent-models
Goloquent
This repo no longer under maintenance, please go to https://github.com/si3nloong/sqlike
Stars: ✭ 16 (-69.23%)
Mutual labels:  eloquent, database
Pecee Pixie
Lightweight, easy-to-use querybuilder for PHP inspired by Laravel Eloquent - but with less overhead.
Stars: ✭ 19 (-63.46%)
Mutual labels:  eloquent, database
Eloquent Driver
A package that allows you to store Statamic entries in a database.
Stars: ✭ 28 (-46.15%)
Mutual labels:  eloquent, database
encryptable
Laravel package for persisting encrypted Model properties, providing decryption when accessed.
Stars: ✭ 26 (-50%)
Mutual labels:  eloquent, eloquent-models
inertiajs-tables-laravel-query-builder
Inertia.js Tables for Laravel Query Builder
Stars: ✭ 391 (+651.92%)
Mutual labels:  eloquent, eloquent-models
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+715.38%)
Mutual labels:  eloquent, database
laravel-quasar
⏰📊✨Laravel Time Series - Provides an API to create and maintain data projections (statistics, aggregates, etc.) from your Eloquent models, and convert them to time series.
Stars: ✭ 78 (+50%)
Mutual labels:  eloquent, eloquent-models
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+917.31%)
Mutual labels:  eloquent, database
Dynamic Data Source Demo
基于事务的读写分离
Stars: ✭ 43 (-17.31%)
Mutual labels:  transaction, database
eloquence
Eloquence provides a cache on top of Eloquent that prevents multiple models being created for a single database row using the Identity Map design pattern.
Stars: ✭ 18 (-65.38%)
Mutual labels:  eloquent, eloquent-models
Libmdbx
One of the fastest embeddable key-value ACID database without WAL. libmdbx surpasses the legendary LMDB in terms of reliability, features and performance.
Stars: ✭ 729 (+1301.92%)
Mutual labels:  transaction, database
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+1721.15%)
Mutual labels:  eloquent, database

Laravel Transactional Model Events

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].