php - 自定义 Artisan 命令以执行多个命令

标签 php laravel laravel-artisan

有没有办法执行一些工匠命令,使用自定义工匠命令,就像我想制作一个名为的自定义命令:

$ php artisan project:init 

这将执行一些命令,如 php artisan migrate:refreshphp artisan db:seedphp artisan config:clear
有没有办法做到这一点?

最佳答案

有两种方法可以对命令进行分组或从另一个命令调用它。
变体 1:
routes/console.php 中创建新的控制台命令.
路线/控制台.php

Artisan::command('project:init', function () {
    Artisan::call('migrate:refresh', []); // optional arguments
    Artisan::call('db:seed');
    Artisan::call('config:clear');
})->describe('Running commands');
变体 2:
根据文档:https://laravel.com/docs/7.x/artisan#calling-commands-from-other-commands
使用命令行创建新命令:
$ php artisan make:command ProjectInit --command project:init
这将创建新文件:App\Console\Commands\ProjectInit在那ProjectInit类(class)handle方法你可以调用另一个命令:
public function handle(){
  $this->call('migrate:refresh', []); // optional arguments
  $this->call('db:seed');
  $this->call('config:clear');
}

关于php - 自定义 Artisan 命令以执行多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61872499/

相关文章:

php - 显示对象的所有公共(public)属性(名称和值)

php - 连接到远程服务器 MySQL 问题

php - 在哪里获取 Google Analytics API access_token?

php - 错误号 : 150 "Foreign key constraint is incorrectly formed" but check everything I know

php - 群组联系人查询在 php 中抛出错误

php - 如何在 laravel 中使用 maatwebsite 设计 excel 表格

php - 拉维尔 4 : many to many (insertion)

Laravel - 在单个 artisan 命令中创建模型、 Controller 和迁移

php - Laravel 框架外的 Artisan::call()

Laravel 维护模式 --allow ip 不工作