Yes, it is possible to use a custom cast against a custom attribute. In the provided code, the outstandingInvoicesAmount method returns an instance of the Attribute class, which is not a field in the database. Therefore, the outstanding_invoices_amount attribute needs to be appended to the model's attributes using the $appends property.
The get method in the Currency cast should be updated to handle the Attribute instance instead of a regular field value. Here's an updated version of the Currency cast:
class Currency implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
if ($value instanceof Attribute) {
$value = $value->get($model);
}
return \NumberFormatter::create('en_GB', \NumberFormatter::CURRENCY)
->formatCurrency($value/100, 'GBP');
}
public function set($model, $key, $value, $attributes)
{
return $value;
}
}
Note that the get method checks if the value is an instance of Attribute and calls its get method to retrieve the actual value. The set method is a no-op and simply returns the value as is.
With these changes, the outstanding_invoices_amount attribute should be correctly cast to a currency value.