这是我组装的Model的构造函数;
public function __construct($attributes = array()) {
parent::__construct($attributes);
var_dump($attributes);
exit;
}
var_dump输出为;
array(1) { ["table"]=> string(14) "participants_2" }
但当我尝试这个;
public function __construct($attributes = array()) {
parent::__construct($attributes);
$this->table = $attributes["table"];
}
我明白;
ErrorException in Participant.php:
Undefined index: table
不管我把$this
行放在parent::__construct
行之前还是之后。这怎么可能是一个未定义的索引?我已经定义了,对吧?
也许这是拉拉威尔的问题?
我也遇到过类似的问题。在我的情况下,第一次调用该方法时没有参数,这导致错误消息,第二次调用带有参数的。检查一下你的情况是否不是这样。
就是这样-您可以添加简单的检查来查看元素isset()
是否
如果你想把一些参数传递给模型构造函数,你应该这样做:
public function __construct($resource, array $attributes = [])
{
parent::__construct($attributes);
$this->table= $resource;
}
不执行
public function __construct($attributes = array()) {
parent::__construct($attributes);
$this->table = $attributes["table"];
}
因为$attributes用于模型实例数据,而不是参数