Is there any way to create a mysql event using laravel migration? If yes how?
-
1You can try `DB::statement('CREATE EVENT....');` – aynber Nov 11 '16 at 19:06
3 Answers
Don't use DB::statement(''), as the event creation needs to be on unprepared mode. Use the following:
DB::unprepared('Your event creation code goes here')

- 546
- 1
- 6
- 10
-
Still we can use `DB::statement` if we are using configuration `PDO::ATTR_EMULATE_PREPARES => true`; I always enable this because emulated prepared statements are safe enough in modern PHP versions. – mpyw Mar 19 '18 at 08:03
You can use DB::statement()
. More details here.
DB::statement('CREATE EVENT my_event ...')

- 3,770
- 4
- 45
- 70
-
1You have to use `DB::unprepared` like @JinIzzraeel mentioned. Else, you get an error like `Cannot execute queries while other unbuffered queries are active`. – windler Mar 13 '18 at 13:24
DB::unprepared(" CREATE EVENT IF NOT EXISTS delete_etiqueta_rendimento_pack ON SCHEDULE EVERY 1 DAY STARTS '2020-04-21 01:00:27' ON COMPLETION NOT PRESERVE ENABLE COMMENT 'Deletes all record redimento_pack from 3 months ago' DO BEGIN DELETE FROM rendimento_packing WHERE data_hora < DATE_SUB(NOW(), INTERVAL 3 MONTH); END ");
If you use DB::statement('') probabli you gonne a see this error: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.