RednBlack RednBlack - 1 year ago 166
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');

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download