Laravel model accessor with attribute name containing a number is ignored

734 Views Asked by At

In Laravel 5.7 in my database I have a property containing a number in its name - item_1_quality. When I create an accessor with method name using camel case it is ignored.

I have tried various combinations the most obvious of which was

public function getItem1QualityAttribute($value)
{
    dd($value);
}

however, it does not work. I tried other possible combinations without success. I am properly calling the property as other accessors in the same model work fine. The problem seems to be related to the naming.

1

There are 1 best solutions below

1
Daniel Plewinski On

I have found a hacky but a working solution which someone might find helpful.

  1. Add attribute to appends

    protected $appends = ['product_1_quality'];
    
  2. Create the following accessor:

     public function getProduct1QualityAttribute()
     {
         $value = $this->attributes['product_1_quality']);
         // do something with your $value and return.
     }
    

The $value as argument will not work - access using the $attributes array.