代码之家  ›  专栏  ›  技术社区  ›  user3691644

将多态模型传递给Laravel-artian命令构造

  •  0
  • user3691644  · 技术社区  · 6 年前

    当使用参数将模型传递给Laravel Artisan控制台命令时,对于多态模型(即参数可以引用 Post , Comment Video )?

    <?php
    
    namespace App\Console\Commands;
    
    use App\User;
    use App\Post;
    use App\Comment;
    use App\Video;
    use Illuminate\Console\Command;
    
    class CreateItem extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'create:item {item}';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Add an item';
    
        protected $item;
    
        /**
         * Create a new command instance.
         */
        public function __construct(VariousModels $item)
        {
            parent::__construct();
    
            $this->item = $item;
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            switch(true){
              case $this->item is instanceof App\Comment:
              break;
              case $this->item is instanceof App\Video:
              break;
              case $this->item is instanceof App\Post:
              break;
              ...
            }
    
        }
    }
    
    0 回复  |  直到 6 年前