$alwaysSelectFields missing from model if not in $defaultFields or ?fields=
When using a model resource, only fields that are properly selected ($defaultFields
or ?fields=
) are added to the model. Fields from $alwaysSelectFields
not.
I understand that this works in terms of final output from the resource, but it'd be nice to have the $alwaysSelectFields
fields available on the model before final output.
If a database table invoices
has the fields
total_cost
total_paid
And the model has an attribute function, eg
public function totalDue(): Attribute
{
return Attribute::get(
fn(): float => $this->total_cost - $this->total_paid
);
}
The resource has:
$alwaysSelectFields = ['total_cost', 'total_paid'];
Then...
-
total_due
can only be accessed via/api/invoices/1
if all three fields are in$defaultFields
. -
total_due
can be accessed via/api/invoices/1?fields=total_cost,total_paid,total_due
-
total_due
cannot be accessed via/api/invoices/1?fields=total_due
I understand that this means the fields would need filtered after (or within) the toArray()
method, so I'm not sure it's feasible or practical. But there may be a way I'm not thinking of.