Expanding on my comment:
Cool
I would create a Currency class, something like this:
class Currency {
$value;
public function __construct($value)
{
$this->value = $value;
}
public function formatted()
{
return '$' . number_format($this->value, 2);
}
// more methods
}
Then override the Model castAttribute methods, to include the new castable class:
protected function castAttribute($key, $value)
{
if (is_null($value)) {
return $value;
}
switch ($this->getCastType($key)) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return (float) $value;
case 'string':
return (string) $value;
case 'bool':
case 'boolean':
return (bool) $value;
case 'object':
return $this->fromJson($value, true);
case 'array':
case 'json':
return $this->fromJson($value);
case 'collection':
return new BaseCollection($this->fromJson($value));
case 'date':
case 'datetime':
return $this->asDateTime($value);
case 'timestamp':
return $this->asTimeStamp($value);
case 'currency': // Look here
return Currency($value);
default:
return $value;
}
}
Simple
Of course you could make things much simpler and just do this in the castAttribute method:
// ...
case 'dollar':
return '$' . number_format($value, 2);
// ...
priceattributes to this class (which would take value as constructor). Then you could do things like$model->price->euror$model->price->formatted.