您有$fillable
和$guarded
来防止大规模分配。但是,我如何保护某些数据库列,使其无法更新?
您可以覆盖模型中的isFillable()
,并使用exists
属性来确定模型是否已经在数据库中(如果您正在创建或更新)。此外,您还需要另一个属性来配置这些属性。我们称之为$guardedForUpdate
:
protected $guarded = ['foo'];
protected $guardedForUpdate = ['bar'];
public function isGuardedForUpdate($key){
return in_array($key, $this->guardedForUpdate) || $this->guardedForUpdate == array('*');
}
public function isFillable($key){
if($this->exists && $this->isGuardedForUpdate($key)){
return false;
}
return parent::isFillable($key);
}
foo
在任何情况下都不会被大规模分配。而CCD_ 7在创建新模型时是可质量分配的,而在更新模型时则不可质量分配。