Shaping a model for JSON has meant a transformer library or toArray() on the model, and the second couples the wire format to the database columns — so adding a column changes the API.
class OrderResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->reference, // not the primary key
'total' => $this->total_cents,
'currency' => $this->currency,
'lines' => OrderLineResource::collection($this->whenLoaded('lines')),
];
}
}
whenLoaded() is the detail worth adopting immediately: it includes the relation only if it was eager loaded, so a resource cannot cause an N+1 by being rendered somewhere that did not load it. Exposing reference rather than the primary key is the other habit — an auto-increment id in a public API leaks volume and invites enumeration.