Jan 8, 2025
0
Level 1
Programmatically Registering and Calling Commands in Laravel 10.42 Using Artisan::call Fails on First Attempt
I have two commands, telegram:command:register and telegram:webhook:setup, which are designed to run only in the command line. However, I want to execute them programmatically. To achieve this, I registered their class files before calling them, as shown below:
$commands = [
WebhookSetupCommand::class,
CommandRegisterCommand::class
];
foreach ($commands as $command) {
$commandInstance = app($command);
Artisan::registerCommand($commandInstance);
}
$option = [
'bot' => $botKey
];
Artisan::call('telegram:command:register', $option);
Artisan::call('telegram:webhook:setup', $option);
Despite this, the commands do not work the first time they are executed. They only work on the second attempt. I would like to know how I can ensure that the commands are immediately available for execution after registration.
Currently, I do not want to define these commands in the Kernel class, as they are already available under Console and are restricted to running only in the command line.
Please or to participate in this conversation.