Create a mysql event with laravel migration?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

DB::statement('CREATE EVENT my_event ...')

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x