How to run laravel migrations without artisan (using code)

对着背影说爱祢 提交于 2019-12-06 07:30:33

问题


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 service was not available for my customers hosting plan, that means I have no access to terminal and can't use artisan. I know how to write a php script that will create sql tables but just before that I was wondering if theres a shortcut to this with laravel since the migrations(tables) are already defined. What I want is like to create a route project.com/run_migrations to do the job! Thanks in advance


回答1:


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 of 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.



来源:https://stackoverflow.com/questions/44608716/how-to-run-laravel-migrations-without-artisan-using-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!