我如何创建运行 php artisan migrate 命令的 php 文件?

how can i create a php file that runs php artisan migrate command?

我想在服务器中 运行s php artisan migrate:fresh (--seed) 命令的 public 目录中创建一个 php 文件。当我将我的项目上传到 FTP 服务器时,我想打开那个 link(例如:www.project.com/migration.php)并且那个文件应该 运行 迁移 and/or 种子文件。可能吗 如果是,我该怎么做?

顺便说一句,我使用 Laravel 7.28 版本

您可以在函数甚至路由中调用这样的 artisan 命令!

public function callArtisanCommands()
{
    \Artisan::call('cache:clear');
    \Artisan::call('view:clear');
    \Artisan::call('route:clear');
    \Artisan::call('migrate:fresh');
}

创建调用 Artisan 命令的路由:

Route::get('migrate', function () {
    $exitCode = Artisan::call('migrate:fresh --seed --force');
});

添加了 --force 参数,因为在 production 环境中需要确认迁移。

有关 Programmatically Executing Commands

的更多信息