RednBlack RednBlack - 7 months ago 63
PHP Question

Laravel get model id inside setAttribute

I have model like this where i want to get a attribute inside SetFooAttribute. but all time $this->getAttribute('id') is empty! is there a way to access id ?

class MyModel extends Model

public static function boot()

$model->uuid = Uuid::generate(4)->string;

public function setFooAttribute($value)
$this->attributes['foo'] = $value.'-'.$this->getAttribute('id');


You should use getKey() method to get the value of the model's primary key. Use it like $this->getKey()