11 months ago

Laravel API Resource - how to assign sequence number for each collection item?

Posted 11 months ago by Tarasovych

I want to get object like this:

"users": [
            "sequence_number": 1
            "name": "Foo"
            "sequence_number": 2
            "eaters": "Bar"

sequence_number might indicate what's the sequence number of certain user. 1st user might have "sequence_number": 1, n-th user might have "sequence_number": n. Can I achieve this without mapping an array, but right inside return?

public function toArray($request)
        return [
            'sequence_number' => //<--here
            'name' => $this->name

Thanks in advance!

Please sign in or create an account to participate in this conversation.