Code Monkey home page Code Monkey logo

laravel-database-trigger's Introduction

Add database trigger to laravel migrations

Software License Build Status StyleCI

Laravel Database Trigger provides a way to add database trigger to laravel migrations just like you would with database table. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Read more about triggers here.

Installation

Laravel Database Trigger requires at least PHP 7.1. This particular version supports laravel at least v5.5. The package currently supports MySQL only.

To get the latest version, simply require the package using Composer:

$ composer require ntimyeboah/laravel-database-trigger

Once installed, if you are not using automatic package discovery, then you need to register the NtimYeboah\LaravelDatabaseTrigger\TriggerServiceProvider service provider in your config/app.php.

Usage

Create a trigger migration file using the make:trigger artisan command. The command requires the name of the trigger, name of the event object table, action timing and the event that activates the trigger.

$ php artisan make:trigger after_users_update

Event object table

The event object table is the name of the table the trigger is associated with.

Action timing

The activation time for the trigger. Possible values are after and before.

after - Process action after the change is made on the event object table.

before - Process action prior to the change is made on the event object table.

Event

The event to activate trigger. A trigger event can be insert, update and delete.

insert - Activate trigger when an insert operation is performed on the event object table.

update - Activate trigger when an update operation is performed on the event object table.

delete - Activate trigger when a delete operation is performed on the event object table.

The following trigger migration file will be generated for a trigger that uses after_users_update as a name, users as event object table name, after as action timing and update as event.

use Illuminate\Database\Migrations\Migration;
use NtimYeboah\LaravelDatabaseTrigger\TriggerFacade as Schema;

class CreateAfterUsersUpdateTrigger extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('after_users_update')
            ->on('users')
            ->statement(function() {
                return '//...';
            })
            ->after()
            ->update();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users.after_users_update');
    }
}

Return the trigger statement from the closure of the statement method.

The following is an example trigger migration to insert into the users_audit table after updating a user row.

...

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('after_users_update')
            ->on('users')
            ->statement(function() {
                return 'insert into `users_audit` (`name`, `email`) values (old.name, old.email);';
            })
            ->after()
            ->update();
    }

...

Testing

Run the tests with:

$ composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

If you discover a security vulnerability within this package, please send an e-mail to Ntim Yeboah at [email protected]. All security vulnerabilities will be promptly addressed.

License

Laravel Database Trigger is licensed under The MIT License (MIT).

laravel-database-trigger's People

Contributors

eboubaker avatar ntimyeboah avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.