Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mokeseven7's avatar

Eloquent Relationships And Pivot Tables

Hey All -

I have read through the Eloquent docs several times, and am struggling to come up with a good way to define the relationships in a way that gets me to my end goal.

I have the following tables (Simplified for the sake of this question)

courses  
  -course_id (pk)  
credits  
   -credit_id (pk)  
   -credit_name  
certifications  
  -certification_id (pk)  
  -certification_name  
course_credit_cert_assoc  
  -row_id (pk)  
  -course_id (fk)  
  -credit_id (fk)  
  -certification_id (fk)  
  -credit_amount   

Here is an example of what some seeded tables would look like:

courses  
course_id   
101  
certifications  
certifcation_id   |   certification_name  
1                                 cool guy  
2                                extra cool guy  
3                                super cool guy  
course_certification_credits
credit_id  |   credit_name   
4                     technical  
5                     leadership  
6                     business  
course_certifications_assoc  
row_id course_id credit_id certification_id num_credits      
1       101         4       1               3.5  
2       101         5       1               1.5  
3       101         6       1               1.5  
4       101         4       2               3.5  
5       101         5       2               1.5  
6       101         6       2               1.5  

The application is a JSON API that returns course data to a client/consumer.

Using Eloquent API resources, this is the desired data structure (with mocked "hydrated" data from the above example")

"certifications" => [
    {
        "cool guy" => [
                {"credit_name" => "technical", "credit_amount" => 1.5 },
                {"credit_name" => "leadership", "credit_amount" => 1.5 },
                {"credit_name" => "business", "credit_amount" => 1.5 },
        ]
    },
    {
        "super cool guy" => [
                {"credit_name" => "technical", "credit_amount" => 1.5 },
                {"credit_name" => "leadership", "credit_amount" => 1.5 },
                {"credit_name" => "business", "credit_amount" => 1.5 },
        ]
    }
]

The controller will perform the initial lookup based on the course_id, which gets passed in the URL. from there, I'll need to retrieve all records from the assoc table with the corresponding course_id, group by the certification id (while displaying the certification name), and then finally lookup all the corresponding credit_id's to get there credit_name values.

I am not super new to eloquent, but this is beyond the type of relationship ive had to define in the past. I am not sure if the association table should be its own model, or whether there is a relationship method i can define directly in the course model (hasManyThrough?).

Or whether i should simply scrap all that, and try to make a database view with all the data i need instead.

Any guidance or thoughts would be greatly appreciated. Thanks very much for your time.

0 likes
0 replies

Please or to participate in this conversation.