代码之家  ›  专栏  ›  技术社区  ›  Malik Muhammad Awan

如何在Laravel Artisan命令中将参数传递给handle()函数?

  •  1
  • Malik Muhammad Awan  · 技术社区  · 5 月前

    我正在Laravel中创建一个自定义的Artisan命令,我需要传递 参数 手柄() 功能。但是,我不确定在运行命令时如何定义和访问这些参数。

    以下是我目前所掌握的:

    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();
        }
    }
    

    我想要什么:

    1:我想传递一个参数 地点 休息室 .

    例子:

    1:php artist应用程序:更新图片位置

    2:php工匠应用程序:更新地点图像休息室

    1 回复  |  直到 5 月前
        1
  •  4
  •   Lajos Arpad    5 月前

    将签名更改为

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

    这就是你取它的方法

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

    由于您希望您的座位或休息室成为参数,因此您无法提前确定是否会收到座位或休息室,因此您需要一些功能来确定 argument 实际上是地方或休息室。