0

Scheduling commands — $schedule->command() in App\Console\Kernel

Intermediate5 min read·lv-25-004

Concept

Artisan::call() allows running Artisan commands programmatically from PHP code — controllers, tests, service classes, or even other commands. This enables building command-driven workflows and administrative UIs.

Artisan::call(string $command, array $parameters = [], ?OutputInterface $outputBuffer = null): Runs synchronously. Returns the exit code (0 = success). Parameters are passed as an associative array.

Parameter format: Arguments are positional by name: ['user' => 5]. Options use --: ['--queue' => true, '--format' => 'text']. Variadic: ['email' => ['a@b.com', 'c@d.com']].

Artisan::queue(string $command, array $parameters): Dispatches the command as a queued job. Does not run synchronously. Returns a PendingDispatch.

Capturing output: Artisan::output() after a call returns the terminal output as a string.

Calling from another command: $this->call('command:name', $params) — runs synchronously and shows output in the parent command's terminal. $this->callSilently('command:name', $params) — suppresses output.

Conditional execution: Unlike scheduled commands, programmatic calls can include runtime conditions from the application state.

Security note: Never expose Artisan::call() to user-controlled input without strict validation. Running arbitrary Artisan commands is equivalent to remote code execution.

Code Example

php
<?php
use Illuminate\Support\Facades\Artisan;

// Basic call — returns exit code
$exitCode = Artisan::call('report:weekly');
$output   = Artisan::output(); // terminal output as string

// With parameters
Artisan::call('report:weekly', [
    'user'     => 42,           // argument
    '--queue'  => true,         // boolean option (flag)
    '--format' => 'text',       // option with value
]);

// With array argument
Artisan::call('users:notify', [
    '--email' => ['alice@example.com', 'bob@example.com'], // variadic
]);

// Queue a command for background processing
Artisan::queue('report:weekly', ['user' => 42])
       ->onQueue('low')
       ->onConnection('redis');

// In a controller — admin panel command runner
class AdminCommandController extends Controller
{
    public function clearCache(Request $request)
    {
        $this->authorize('run-maintenance-commands');
        Artisan::call('cache:clear');
        return response()->json(['output' => Artisan::output()]);
    }

    public function runMigrations(Request $request)
    {
        $this->authorize('run-migrations');
        $exitCode = Artisan::call('migrate', ['--force' => true]);
        return response()->json([
            'success' => $exitCode === 0,
            'output'  => Artisan::output(),
        ]);
    }
}

// Calling from another command
class WeeklyMaintenanceCommand extends \Illuminate\Console\Command
{
    protected $signature = 'maintenance:weekly';

    public function handle(): int
    {
        $this->call('cache:clear');              // output shown in parent
        $this->callSilently('optimize');         // output suppressed
        $this->call('report:weekly', ['--queue' => true]);
        $this->info('Weekly maintenance complete.');
        return self::SUCCESS;
    }
}