Laravel Tutorials

Retrieving Command Parameters in Laravel Artisan

Published
Retrieving Command Parameters in Laravel Artisan image

Laravel provides straightforward methods for accessing arguments and options within your custom Artisan commands. These built-in helpers simplify parameter retrieval and validation.

Access individual parameters using dedicated methods:

$username = $this->argument('username');

$environment = $this->option('env');

You can also retrieve all parameters at once when building dynamic command processors or debugging utilities.

$allArguments = $this->arguments();

$allOptions = $this->options();

Here's a backup management command that demonstrates comprehensive parameter handling:

class BackupDatabase extends Command
{
    protected $signature = 'backup:database 
        {database : Database name to backup}
        {--path= : Custom backup directory}
        {--compress : Compress the backup file}
        {--exclude=* : Tables to exclude from backup}
        {--S|silent : Run without output}';

    public function handle()
    {
        $databaseName = $this->argument('database');
        $backupPath = $this->option('path') ?? storage_path('backups');
        $shouldCompress = $this->option('compress');
        $silent = $this->option('silent');
        
        if (!$silent) {
            $this->line("Starting backup for database: {$databaseName}");
            $this->line("Backup location: {$backupPath}");
        }

        if ($shouldCompress) {
            $this->info('Compression enabled');
        }

        $excludedTables = $this->option('exclude');
        if (!empty($excludedTables)) {
            $this->warn('Excluding tables: ' . implode(', ', $excludedTables));
        }

        $allParams = $this->arguments();
        $allFlags = $this->options();
        
        if ($this->option('verbose')) {
            $this->table(['Parameter Type', 'Count'], [
                ['Arguments', count($allParams)],
                ['Options', count($allFlags)]
            ]);
        }
    }
}

Laravel's parameter access methods enable you to build robust command-line tools that handle complex input scenarios with minimal code complexity.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article
Session Replay for Laravel: Self-Hosted Browser Recordings image

Session Replay for Laravel: Self-Hosted Browser Recordings

Read article
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article