Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadrix's avatar
Level 12

Question to API Resource Collection - How to use own names

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)
0 likes
1 reply

Please or to participate in this conversation.