Передача полиморфной модели в командную конструкцию Laravel artisan - PullRequest
0 голосов
/ 25 июня 2018

При передаче модели в консольную команду 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;
          ...
        }

    }
}
...