如何在没有工匠的情况下运行laravel迁移(使用代码) [英] How to run laravel migrations without artisan (using code)
问题描述
我最近在共享主机上托管了一个laravel项目(面向客户),在尝试通过ssh无法访问服务器后,我联系了主机,主机通知我我的客户托管计划无法使用ssh服务,这意味着我无权使用终端,也无法使用工匠.我知道如何编写一个将创建sql表的php脚本,但是在此之前,我想知道laravel是否存在对此的快捷方式,因为已经定义了迁移(表).我想要的是像创建路径project.com/run_migrations
来完成这项工作!预先感谢
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
推荐答案
您可以像这样在PHP中轻松创建一个小型Artisan脚本:
You can easily create a small Artisan script within PHP like this:
Artisan::call('migrate');
这等于php artisan migrate
.在您要运行迁移的任何地方使用它.
This equals php artisan migrate
. Use it anywhere you want to run your migrations.
如果您处于生产模式(如果.env
文件中的APP_ENV=production
),则必须强制进行迁移,以防您要进行更改.您可以按照以下步骤进行操作:
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 ]);
这等同于将--force标志添加为php artisan migrate --force
.
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 ]);
});
如果您对创建Web安装程序感兴趣,则可能对此软件包感兴趣:
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.
这篇关于如何在没有工匠的情况下运行laravel迁移(使用代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!