1

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 Answer 1

3

You could create a resource that works for a single object, then use resource collection for the types to loop over the documents.

https://laravel.com/docs/8.x/eloquent-resources#resource-collections

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.