How about;
protected $appends = [];
public function getCoNameAttribute()
{
return stripBrackets($this->attributes['coName']);
}
Not sure why it would be different to $this->coName but its the way I always do it
We have a field in our company DB table coName that needs to be cleaned before it is displayed. An example value would be "Some Company [HQ]". We would like to strip off the bracketed information so we yield "Some Company". I have a helper function stripBrackets() that handles this fine.
I can create an accessor getDisplayNameAttribute(), add it to the $appends array in the model and it works fine.
protected $appends = ['displayName'];
public function getDisplayNameAttribute()
{
return stripBrackets($this->coName);
}
// "{..."displayName":"Some Company",...}"
If I change the accessor to getCoNameAttribute() and update the $appends array I get a null value
protected $appends = ['coName'];
public function getCoNameAttribute()
{
$this->attributes['coName'] = stripBrackets($this->attributes['coName']);
}
// "{..."coName":null,...}"
// I also tried:
protected $appends = [];
public function getCoNameAttribute()
{
return stripBrackets($this->coName);
}
// and:
protected $appends = [];
public function getCoNameAttribute($value)
{
return stripBrackets($value);
}
Can anyone point me in the right direction? I can create the additional element displayName and call it a day, but I would prefer to simply modify the original coName value as we will never want to display the bracketed information to a user.
Please or to participate in this conversation.