简体   繁体   English

在不可填写的字段上进行批量分配

[英]Mass assignment working on non-fillable fields

I'm writing some unit tests for a simple model which only has two fields: question_id and title. 我正在为一个只有两个字段的简单模型编写一些单元测试:question_id和title。

I've set the fillable array on the model to include only the title: 我在模型上设置了可填充数组,只包含标题:

protected $fillable = array('title');

I've also created the following unit test: 我还创建了以下单元测试:

    /**
     * @expectedException Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function testAnswerQuestionIdCanNotBeMassAssigned()
    {
        $params = ['question_id' => 1, 'title' => 'something'];
        $answer = new Answer($params);
    }

However,no exception is thrown, causing the test to fail. 但是,不会抛出异常,导致测试失败。

Am I missing something here? 我在这里错过了什么吗?

Any advice appreciated. 任何建议表示赞赏

Thanks. 谢谢。

you can see why in the fill method of the Model 你可以在模型的填充方法中看到原因

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM