我有这样的模型,我想在SetFooAttribute中获得一个属性。但所有时间$ this-> getAttribute('id')是空的!有没有办法访问身份证?Laravel在setAttribute中获取模型ID
class MyModel extends Model
{
public static function boot()
{
parent::boot();
static::created(function($model)
{
$model->uuid = Uuid::generate(4)->string;
$model->save();
});
}
public function setFooAttribute($value)
{
$this->attributes['foo'] = $value.'-'.$this->getAttribute('id');
}
在什么情况下使用'$ model-> foo ='somevalue''?如果在模型创建之前使用它,则尚未设置ID。 –
是的,我在创建模型之前使用它。 – RednBlack