Laravel 9 - 3 table pivot nested response

Viewed 18

I have the following models.

  • Service
  • Location
  • Document

Each service can exist in multiple locations.
Each Document can exist in multiple services and location.

I'd like to query the Service model and get a nested response as below.

Service 1

  • Location 1
    • Document 1
    • Document 2
  • Location 1
    • Document 1
    • Document 3

-Service 2

  • Location 4
    • Document 1
    • Document 2
    • Document 5

What's the best way to structure my databases and best practices for inserting and querying the data. I'm not expecting high volumes of data (1 new record created an hour at most), but I still like to follow best practices and guidelines.

I have tried creating a pivot table called document_location_service and I have three columns (document_id, location_id, and service_id). But when I query the table I get duplicate results in my JSON.

0 Answers
Related