我有這樣的模型,我想在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