Level 12
I think that is what I need in my case: https://medium.com/@dinotedesco/laravel-api-resources-what-if-you-want-to-manipulate-your-models-before-transformation-8982846ad22c
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I'm learning about API Resource and I need this for using HashIds.
I'm a bit confused how I can use my "own" names in a collection.
In single models I can say:
public function toArray($request)
{
return [
'myownid' => $this->id,
'username' => $this->name,
'awesomeemail' => $this->email,
];
}
In collection I need to write:
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
But how can I change the attribute names?
So I can use: 'myownid' => Hashids::encode($this->id),
My collection result is quite big:
$order->with(['items', 'items.offer', 'items.offer.offering', 'transaction', 'billing', 'billing.buyer', 'seller'])
->filter($filters) //query filter
->paginate(10)
Please or to participate in this conversation.