[英]Mass assignment working on non-fillable fields
我正在为一个只有两个字段的简单模型编写一些单元测试:question_id和title。
我在模型上设置了可填充数组,只包含标题:
protected $fillable = array('title');
我还创建了以下单元测试:
/**
* @expectedException Illuminate\Database\Eloquent\MassAssignmentException
*/
public function testAnswerQuestionIdCanNotBeMassAssigned()
{
$params = ['question_id' => 1, 'title' => 'something'];
$answer = new Answer($params);
}
但是,不会抛出异常,导致测试失败。
我在这里错过了什么吗?
任何建议表示赞赏
谢谢。
你可以在模型的填充方法中看到原因
public function fill(array $attributes)
{
$totallyGuarded = $this->totallyGuarded();
foreach ($this->fillableFromArray($attributes) as $key => $value)
{
$key = $this->removeTableFromKey($key);
// The developers may choose to place some attributes in the "fillable"
// array, which means only those attributes may be set through mass
// assignment to the model, and all others will just be ignored.
if ($this->isFillable($key))
{
$this->setAttribute($key, $value);
}
elseif ($totallyGuarded)
{
throw new MassAssignmentException($key); <--- only if totally guarded
}
}
return $this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.