Reputation: 1075
I am trying to define an accessor in my User model for a field which already exists. This field is called address_line_1
in my DB. However because the field name contains a number, I am unsure of how to get Laravel to recognise there is an underscore after 'line.'
Usually to define an underscore, you would camel case but in this case you can't. I have checked the Laravel documentation however this issue is not mentioned. Below is my code currently:
public function getAddressLine1Attribute($value){
return empty($value) ? '' : decrypt($value);
}
I have also tried the function name getAddress_Line_1Attribute
and getAddressLine_1Attribute
but this does not work.
How can I get around this?
Upvotes: 1
Views: 1839
Reputation: 653
Laravel 5.7 calls this function to check if the get mutator exists:
/**
* Determine if a get mutator exists for an attribute.
*
* @param string $key
* @return bool
*/
public function hasGetMutator($key)
{
return method_exists($this, 'get'.Str::studly($key).'Attribute');
}
'get'.Str::studly('address_line_1').'Attribute' === 'getAddressLine1Attribute'
So it seems your method name is correct.
Upvotes: 3