Laravel 5.3 – Creating MySQL Triggers from Migration with example

Laravel 5.3 – Creating MySQL Triggers from Migration with example

In this post we will give you information about Laravel 5.3 – Creating MySQL Triggers from Migration with example. Hear we will give you detail about Laravel 5.3 – Creating MySQL Triggers from Migration with exampleAnd how to use it also give you demo for it if it is necessary.

Laravel 5.3 – Creating MySQL Triggers from Migration with example


In this tutorial, you will learn how to work with MySQL in Laravel 5.

Trigger is what it is only stored program and once you write a trigger against any events then whenever that events occur then triggers are automatically executed.

Normally we write DML triggers that means whenever user want to insert update or delete anything from MySQL database then DML triggers fire.

To add triggers to database schema, we write create statement like this :


CREATE
    TRIGGER 'trigger_name' BEFORE/AFTER INSERT/UPDATE/DELETE
    ON 'database'.'table-name'
    FOR EACH ROW BEGIN
		-- trigger body
		-- write query that will be executed against specified events
		-- inserted/updated/deleted row
    END;


In Laravel, we will use DB::unprepared() method to write triggers query to add triggers to database schema.


I am going to create triggers from migration file so first i need to create migration file using following command :

php artisan make:migration create_trigger

Now you will see a migration file in database/migrations directory, open this migration file and add following code :


Migration: CreateTrigger

I create a trigger to insert user role id to assign user with default role whenever new user will be inserted.


  1. <?php
  2. use IlluminateSupportFacadesSchema;
  3. use IlluminateDatabaseSchemaBlueprint;
  4. use IlluminateDatabaseMigrationsMigration;
  5. class CreateTrigger extends Migration
  6. {
  7. public functionup()
  8. {
  9. DB::unprepared('CREATE TRIGGER user_default_role AFTER INSERT ON 'users' FOR EACH ROW
  10. BEGIN
  11. INSERT INTO 'user_role' ('role_id', 'user_id', 'created_at', 'updated_at') VALUES (3, NEW.id, now(), null);
  12. END');
  13. }
  14. public functiondown()
  15. {
  16. DB::unprepared('DROP TRIGGER 'user_default_role'');
  17. }
  18. }
<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateTrigger extends Migration
{
    public function up()
    {
          DB::unprepared('CREATE TRIGGER user_default_role AFTER INSERT ON 'users' FOR EACH ROW
                BEGIN
                   INSERT INTO 'user_role' ('role_id', 'user_id', 'created_at', 'updated_at') VALUES (3, NEW.id, now(), null);
                END');
    }
    public function down()
    {
       DB::unprepared('DROP TRIGGER 'user_default_role'');
    }
}

Run Migration:

php artisan migrate

Let you know only one thing about trigger, trigger will be fired when specified action is executed for the table.

Hope this code and post will helped you for implement Laravel 5.3 – Creating MySQL Triggers from Migration with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

22  +    =  28

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US