Creating MYSQL Procedure in Laravel 4 Migrations

后端 未结 2 1903
悲哀的现实
悲哀的现实 2020-12-03 11:01

Is there a way to generate stored MYSQL procedures in a Laravel 4 migration?

For example, here\'s a simple procedure generation query stored as a string (via a Hered

2条回答
  •  心在旅途
    2020-12-03 11:49

    For fellow dev looking for the link in laravel mentioned by @Johannes.

    Any way to create MYSQL Procedures in Migrations? answered by @aheissenberger.

    I use this and it works well for me:

    public function up() {            
        DB::unprepared('CREATE PROCEDURE get_highscore() BEGIN SET time_zone = \'Europe/Berlin\'; SET @refscore :=0; SELECT * FROM test; END');
    }
    
    public function down() {
        DB::unprepared('DROP PROCEDURE IF EXISTS get_highscore');
    }
    

    To call a procedure in your code:

    DB::unprepared('CALL get_highscore()');
    

    if you expect a resulting table:

    DB::statement('CALL update_highscore()');
    

    if you expect variables:

    DB::statement('CALL update_ranking(3,10,@olduser,@newuser)');
    $dberg = DB::select('select @olduser as old, @newuser as new');
    

提交回复
热议问题