Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

397
Views
Laravel return custom api resource

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!