Laravel return custom api resource

Viewed 771

I am currently creating an API to display the count of documents by types.

This is my type table:

id name
1 abc
2 bcd
3 efd

And this is my documents tables:

id document_name type_id
1 abc 1
2 bcd 2
3 efd 3

I want to return my Resource like this:

'title' => 'Title',
'types' => [
    [
        'label' => 'abc',
        'total' => 1
    ],
    [
        'label' => 'bcd',
        'total' => 2
    ],
    [
        'label' => 'efd',
        'total' => 3
    ]
]

How can I create a custom resource with looping?

1 Answers
Related