首页 > 解决方案 > 自动运行 php artisan ide:models

问题描述

我通过 make:command 创建一个命令

php artisan make:command ResetDBCommand

然后我想在处理程序中运行这两个命令

php artisan ide:models
php artisan db:seed

但是,我无法通过以下代码自动触发这两个命令

Artisan::call('db:seed');
$console->writeln('db:seed done.');

Artisan::call('ide:models--force');
$console->writeln('ide:models done.');

错误:

The command "ide:models--force" does not exist.

我怎样才能做到这一点?

标签: laravellaravel-artisan

解决方案


正确的命令是ide-helper:models你可以确认这一点,如果你这样做:

php artisan help ide:models

你得到:

[...]
用法:
ide-helper:models [options] [--] [<model>...]

这表明 Laravel 在命令行中调用时会自动解析该命令。然而,当以编程方式调用它时,这种解析机制并不存在。

另一个问题是这--force不是一个有效的选项,ide-helper:models但您可以这样做:

Artisan::call('db:seed');
$console->writeln('db:seed done.');

// Uncomment one of the two
// Artisan::call('ide-helper:models --nowrite'); // Only write metadata in the _ide_helper_models.php file
// Artisan::call('ide-helper:models --write'); // Write metadata on models
// ------
$console->writeln('ide:models done.');

相应地选择您喜欢的任何一个

在 Laravel 8 中测试了上述内容


推荐阅读