Search code examples
phplaravel-5

laravel 5 model method not working


I have a table with rows with column contain string "this\dIs\dA\dString"

$callPlans = CustomerCallPlan::where('customer_id', $customer->id)->get();

I get the values like above and expected string 'thisXIsXAXString' as you guess I replace '\d' with 'X'. to do this I use method below inside model class.

class CustomerCallPlan extends Model
{
    protected $table = 'customer_callplan';

    protected $fillable = [
        'template',
        'priority',
        'customer_id',
        'strip',
        'add_number',
        'actiontype',
        'data'
    ];

    public function getNumbertemplateAttribute() {
        return str_replace('\d', 'X', $this->attributes['template']);
    }
}

But somehow data comes to model without replaced.. what might be cause this ??


Solution

  • This is called an accessor and it'll automatically be called by Eloquent when attempting to retrieve the value. The method name should be the camel cased name of the column you wish to access, prepended by get and followed by Attribute, for example getColumnNameAttribute() will take the column colum_name.