laravellaravel-artisan

Running php artisan ide:models automatically


I create a command through make:command

php artisan make:command ResetDBCommand

Then I would like to run the two command in handler

php artisan ide:models
php artisan db:seed

However, it's not work for me to fire the two command automatically through the below codes

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

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

Error:

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

how can I do that?


Solution

  • The correct command is ide-helper:models you can confirm this if you do:

    php artisan help ide:models
    

    You get:

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

    which indicates that Laravel does automatically resolve this command when called in the command line. However such resolution mechanism does not exist when calling it programmatically.

    Another issue is that --force is not a valid option in ide-helper:models here's what you can do though:

    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.');
    

    Pick whichever one you prefer accordingly

    Tested the above in Laravel 8