How to run laravel migrations without artisan (using code)

后端 未结 1 2069
无人及你
无人及你 2021-02-09 20:09

I recently hosted a laravel project (for a customer) on shared hosting, after failed attempts to get access to the server via ssh I contacted the host who informed me that ssh

1条回答
  •  隐瞒了意图╮
    2021-02-09 20:26

    You can easily create a small Artisan script within PHP like this:

    Artisan::call('migrate');
    

    This equals php artisan migrate. Use it anywhere you want to run your migrations.

    If you are in production mode (if APP_ENV=production inside your .env file) then you would have to force the migration in case you want to allow to make changes. You can do it as follows:

    Artisan::call('migrate', ["--force" => true ]);
    

    This equals adding the --force flag a la php artisan migrate --force.

    To answer your specific question though, create a route like this:

    Route::get('/run-migrations', function () {
        return Artisan::call('migrate', ["--force" => true ]);
    });
    

    If you are interested in creating a web installer, you might be interested in this package:

    https://github.com/Froiden/laravel-installer

    Check out the code to see how he handles migrations and seeds etc.

    0 讨论(0)
提交回复
热议问题