2

我创建了如下命令:

<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class SchedulesCommand extends Command {

    protected $name = "mod:check";

    protected $description = "Checks the Database for any upcoming schedules.";

    protected $mod;

    public function __construct (Model $mod){
        $this->mod = $mod;
    }

    public function fire(){
        $this->line('Checking the database');
        $this->mod->f1 = 1;
        $this->mod->f2 = "test";
        $this->mod->f3 = 'Command';
        $this->mod->save();
    }
?>

我收到以下错误-不知道为什么-

{"error":{"type":"ErrorException","message":"Invalid argument supplied for foreach()","file":"\/home\/test\/Documents\/account1\/PHP Scripts\/project\/vendor\/symfony\/console\/Symfony\/Component\/Console\/Application.php","line":409}}

有人可以告诉我可能出了什么问题吗?

这是我的artisan.php文件:

 $mod = new Model;
 $artisan->add(new SchedulesCommand($mod));
4

1 回答 1

3

创建 Laravel 命令时,您应该始终调用父构造函数。

在您的构造函数中添加parent::__construct()第一行,如下所示:

public function __construct (Model $mod)
{
    parent::__construct();
    $this->mod = $mod;
}

请记住,调用父构造函数允许 Laravel 在控制台命令上配置名称、描述和参数,以使我们更轻松一些。

于 2013-08-19T19:45:36.723 回答