当使用参数将模型传递给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;
...
}
}
}