Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

399
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda