So let’s say I have model Product with getPriceAttribute accessor like this
public function getPriceAttribute()
{
return number_format($this->price);
}
So on every response, I will get price with formatted number. But in some case, I need to return the unformatted number. Example:
$product = Product::find(1);
$product->price = preg_replace('/[^0-9]/', '', $product->price);
But the result still gives me the formatted number instead of unformatted number.
I know I can create separate accessor attribute like getUnformattedPriceAttribute for unformatted number, but the client need to receive the same price attribute.
And the work around that I do for now is to convert the model into array first using ->toArray() and then set the key price to unformatted one. But I want to know if It is possible to override this accessor behaviour when It is still in Laravel Model.
Thank you!