Laravel Tutorials

Accepting Multiple Parameters in Laravel Commands

Published
Accepting Multiple Parameters in Laravel Commands image

Laravel Artisan commands support array-based parameters, enabling you to process multiple values efficiently. This functionality streamlines batch operations and complex command workflows.

Use the asterisk notation to accept multiple argument values:

protected $signature = 'process:files {filename*}';

For optional multiple arguments, combine the question mark with asterisk:

protected $signature = 'process:files {filename?*}';

Multiple option values follow a similar pattern, allowing repeated flag usage in command execution.

protected $signature = 'process:files {--format=*}';

Here's a content publishing system that demonstrates comprehensive multi-value parameter handling:

class PublishContent extends Command
{
    protected $signature = 'content:publish 
        {articles*: Article IDs to publish}
        {--C|category=*: Categories to include}
        {--platform=*: Publishing platforms}
        {--schedule=?: Publication schedule}';

    public function handle()
    {
        $articleIds = $this->argument('articles');
        $categories = $this->option('category');
        $platforms = $this->option('platform');
        $schedule = $this->option('schedule');

        $this->line("Processing " . count($articleIds) . " articles for publication");

        foreach ($articleIds as $articleId) {
            $article = Article::findOrFail($articleId);
            
            if ($categories && !in_array($article->category_id, $categories)) {
                $this->warn("Skipping Article {$articleId} - category mismatch");
                continue;
            }

            foreach ($platforms as $platform) {
                $this->publishToplatform($article, $platform, $schedule);
                $this->info("Published Article {$articleId} to {$platform}");
            }
        }

        $this->info("Publication process completed successfully");
    }

    private function publishToPlatform($article, $platform, $schedule = null)
    {
        $publishedAt = $schedule ? Carbon::parse($schedule) : now();
        
        Publication::create([
            'article_id' => $article->id,
            'platform' => $platform,
            'published_at' => $publishedAt,
            'status' => 'published'
        ]);
    }
}

Array parameter support transforms simple commands into powerful batch processing tools, ideal for administrative tasks and data management operations.

Harris Raftopoulos photo

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

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
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