Laravel, create MySQL trigger from Migration

Run composer dumpautoload in the root (the same place as artisan) should make it work.


There was issue with class naming.

Correct class name could help OR do as I did, Copy your working trigger code temporary in notepad/text. Delete the old migration trigger file and generate new one.

Note: By the way the same solution is valid for Laravel 4.x and Laravel 5.x

In Laravel 4

php artisan generate:migration create_trigger

In Laravel 5

php artisan make:migration create_trigger

After it was generated I copy and paste the same Trigger code from my notepad/text and it works just fine.

Here is the final working code for creating trigger through migration.

it works both with RAW and UNPREPARED method.

<?php

use Illuminate\Database\Migrations\Migration;

class CreateTrigger extends Migration {

    public function up()
    {
        DB::unprepared('
        CREATE TRIGGER tr_User_Default_Member_Role AFTER INSERT ON `users` FOR EACH ROW
            BEGIN
                INSERT INTO role_user (`role_id`, `user_id`, `created_at`, `updated_at`) 
                VALUES (3, NEW.id, now(), null);
            END
        ');
    }

    public function down()
    {
        DB::unprepared('DROP TRIGGER `tr_User_Default_Member_Role`');
    }
}

Note: This is just example to demonstrate the concept