0

Is there any way to create a mysql event using laravel migration? If yes how?

computingfreak
  • 4,939
  • 1
  • 34
  • 51
Bruno Erceg
  • 73
  • 1
  • 1
  • 7

3 Answers3

4

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')

Jin
  • 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
2

You can use DB::statement(). More details here.

DB::statement('CREATE EVENT my_event ...')
Wistar
  • 3,770
  • 4
  • 45
  • 70
  • 1
    You 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
1

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.