Many to Many in Laravel

-1

I wanted to ask you the following question:

It turns out that I have a few tables that require a "many to many" relation (eg, users and roles, assuming primary key for both as 'id').

The issue is that I have the doubt as a "best practice" (using laravel) if I create the pivot table role_user with composite primary key (role_id and user_id) or create the column 'id' accompanied by the foreign keys " role_id and user_id ").

I understand that the second option could generate problems with the coherence of the data (being able to generate repeated combinations), but I evaluate that option considering that laravel does not let me handle the composite keys very well.

What would be a good practice for this case and the like? .

Beforehand, thank you very much!

    
asked by Camilo Araya 07.11.2018 в 17:00
source

1 answer

-1

The intermediate table or also called "pivot table" or "pivot table" normally serves only to maintain the relationship between two models, so there is no need to put an id as a primary key incremental auto since nothing is done directly with that table, now that if your intermediate table is part of another relationship with another table outside the two that communicates, if it would be convenient to take it.

Laravel's documentation does not say anything about it but something that comes close is the following:

  

"By default, only model keys will be present in the pivot table.

     

If your pivot table contains extra attributes, you must specify them when you define the relationship. "

    
answered by 07.11.2018 в 23:32