class User extends Eloquent {
protected $table = 'my_users';
// The 'my_users' table does *not* provide this column.
protected $computedValue;
public function doSomething() {
// Anyway, for some reason, i need the property to exist in the model.
$this->computedValue = 'foo';
}
}
当我这样做时User::save()
,会触发错误,因为 Eloquent 正在尝试 insert into my_users (computedValue) values('foo')
实际上,我在触发保存之前取消设置属性。是否有其他方法(除了监听保存事件以取消设置之外),例如
protected $discard = array('computedValue', 'anyOtherFieldNotToMap');
?
最佳答案
您可以尝试为该成员变量指定默认值。
protected $computedValue = false;
引用 Laravel 论坛 Setting transient properties on Eloquent models without saving to database?
似乎对那张海报有用。
关于php - Laravel 4,如何防止 Eloquent 模型处理某些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23344089/