How to add mysql trigger from migrations in Laravel 5?
In this post we will give you information about How to add mysql trigger from migrations in Laravel 5?. Hear we will give you detail about How to add mysql trigger from migrations in Laravel 5?And how to use it also give you demo for it if it is necessary.
Sometimes you require to add trigger in your mysql database of laravel 5. but you think how to create trigger using laravel migration because laravel not provide special function like insertTrigger and something so it problem to create direclty, But laravel DB::unprepared() through we can create trigger for database.
In Following example you can see how to create migration and create trigger code and how to write drop trigger code, so let’s do it this way.
Create Migration:
php artisan make:migration add_trigger
Migration:
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class AddTrigger extends Migration
{
public function up()
{
DB::unprepared('CREATE TRIGGER add_Item_city AFTER INSERT ON 'items' FOR EACH ROW
BEGIN
INSERT INTO 'items_city' ('item_id') VALUES (NEW.id);
END');
}
public function down()
{
DB::unprepared('DROP TRIGGER 'add_Item_city'');
}
}
Run Migration:
php artisan migrate
Try this….
Hope this code and post will helped you for implement How to add mysql trigger from migrations in Laravel 5?. 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