3 years ago

Pivot table for multiple many-to-many relationships

Posted 3 years ago by ivan2nn

I am no expert with databases and I have to face thsir problem. I know how to make a pivot table for many-to-many relationship between two models. I would like to know how to behave when i have multiple relationships in the same pivot table. This is an example:

car_code    |   car_status  |   car_trend   |   car_region
1001        |   FV      |       N       |       MMED
1008         |      FV      |   N        |  MMED
1012         |      U2      |   =        |  MMED
1013         |      FV      |   N        |  ALP
1014         |      FV      |   N        |  ALP
1014         |      U1      |   x        |  CON

They are all primary keys,.... I don't know how to design a table like this starting from what i have to set in the model (or the name of the table which I understood it must be made by the two model having the many-to-many realtionship)

something like inside Car model

private function statuses() {
return $this->hasMany('App\Status')    // and here i have to identify the table?

private function trends() {
    return $this->hasMany('App\Trend') // and here I have to identify the same table as before?

private function regions() {
    return $this->hasMany('App\Region') // same as before

and do i have to do the same for the other classes?

Thank you

Please sign in or create an account to participate in this conversation.