2

I am creating a custom Artisan command in Laravel, and I need to pass parameters to the handle() function. However, I'm not sure how to define and access these parameters when running the command.

Here’s what I have so far:

class UpdatePlacesImages extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:update-places-images';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Fetch places data(specially images) from Places API and save it in the goog_data field';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        /* We are updating google_data field by using two separate functions 
        1: updatePlacesImages()
            Updating ""google_data"" field using ""place_id"" for all places ""Expect Lounges"" because Lounges places
            are stored in table name ""lounges"" and DB name ""newairport"" while the remaining all places are 
            saved in table name ""places"" and DB name ""airport_remote  
            
        2: updateLoungesImages()
            Updating ""google_data"" field using ""place_id""  only for lounges place
        */

        $this->updatePlacesImages();
        $this->updateLoungesImages();
    }
}

What I Want:

1: I want to pass a parameter places or lounges.

Example:

1: php artisan app:update-places-images places

or

2: php artisan app:update-places-images lounges

1
  • 1
    The answer below is correct; you don't pass anything to handle(), but rather via $signature then either $this->argument() or $this->option(). Full details in the documentation for the Artisan Console: laravel.com/docs/11.x/artisan Commented Dec 19, 2024 at 13:46

2 Answers 2

5

Change your signature to

protected $signature = 'app:update-places-images {argument}';

This is how you can fetch it

public function handle(): void
{
    $argument = $this->argument('argument');
}

Since you want your places OR lounges to be the parameter, you cannot in advance determine whether you will receive places or lounges, therefore you'll need some functionality based on which you will be able to determine whether argument is actually places or lounges.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for helping. Can you also tell me how to make parameter optional?
@MalikMuhammadAwan you can specify an optional argument via protected $signature = 'app:update-places-images {argument?}';
Thanks bro for your help.
@MalikMuhammadAwan happy to help
0

You can use accept options from the signature like this:

protected $signature = 'app:update-places-images {places} {lounges}';

Then retrieve them like below:

public function handle(): void
{
   $places = $this->option('places');
   $lounges = $this->option('lounges');

}

Alternatively, you retrieve everything as an array by doing this:

public function handle(): void
{
   $options = $this->options();
}

You can read more here: https://laravel.com/docs/11.x/artisan#options

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.