【问题标题】:how can i create a php file that runs php artisan migrate command?如何创建一个运行 php artisan migrate 命令的 php 文件?
【发布时间】:2021-03-29 20:14:54
【问题描述】:

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

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

【问题讨论】:

  • 您可能应该将其添加到您的部署脚本中,而不是将其添加到端点(您到底为什么要这样做?)

标签: php laravel laravel-migrations


【解决方案1】:

你可以在函数甚至路由中调用这样的工匠命令!

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

【讨论】:

    【解决方案2】:

    创建一个调用 Artisan 命令的路由:

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

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

    更多信息Programmatically Executing Commands

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 2017-05-31
      • 2020-11-11
      • 1970-01-01
      • 2017-09-27
      • 2018-07-28
      • 2019-10-29
      • 2019-01-12
      • 2023-03-13
      相关资源
      最近更新 更多