Laravel the right way for Many to Many table structurePostgreSQL “DESCRIBE TABLE”How to 'insert if not exists' in MySQL?Should a junction table for a many-to-many relationship have a surrogate primary key?Laravel and pivot table to relate different modelLaravel - Many to Many relationshipRelations between not directly related tables - Laravel EloquentRetrieve distant relation through has-many-through for many-to-many relation in LaravelLaravel model with multiple belongsToMany returning nullHow to get data based in comparing value in pivote table with value in required table in laravel 5.6?Laravel many-to-many relation with custom table names and IDs

What is my malfunctioning AI harvesting from humans?

Three legged NOT gate? What is this symbol?

If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?

Is it incorrect to write "I rate this book a 3 out of 4 stars?"

Double redundancy for the Saturn V LVDC computer memory, how were disagreements resolved?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

How does 'AND' distribute over 'OR' (Set Theory)?

Should I ask for permission to write an expository post about someone's else research?

How does "Te vas a cansar" mean "You're going to get tired"?

Who are these characters/superheroes in the posters from Chris's room in Family Guy?

Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?

Was the 2019 Lion King film made through motion capture?

Plausibility of Ice Eaters in the Arctic

Generate Brainfuck for the numbers 1–255

Blocking people from taking pictures of me with smartphone

Is refreshing multiple times a test case for web applications?

How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?

Is TA-ing worth the opportunity cost?

Generating function of ordered partitions

Should you play baroque pieces a semitone lower?

What does Apple mean by "This may decrease battery life"?

Simple Stop watch which i want to extend

Acceptable to cut steak before searing?

Can a fight scene, component-wise, be too complex and complicated?



Laravel the right way for Many to Many table structure


PostgreSQL “DESCRIBE TABLE”How to 'insert if not exists' in MySQL?Should a junction table for a many-to-many relationship have a surrogate primary key?Laravel and pivot table to relate different modelLaravel - Many to Many relationshipRelations between not directly related tables - Laravel EloquentRetrieve distant relation through has-many-through for many-to-many relation in LaravelLaravel model with multiple belongsToMany returning nullHow to get data based in comparing value in pivote table with value in required table in laravel 5.6?Laravel many-to-many relation with custom table names and IDs






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm having a many to many relation. Lets say that I have Many users that can be in Many groups and the other way around.



I wan't to make a table that contains the following columns



group_user



id / name / user_id / group_id



Since I have only 3 groups Junior, Middle and Senior I don't want to make another separated table for them. My idea here is to make the following approach:



  1. Creating 3 records in the same table group_user with the following data

id / name / user_id / group_id



1 / Junior / null / null



2 / Middle / null / null



3 / Senior / null / null



So now when I want to insert a group_id I will use this 3 ID's that I just created with NULL user_id and group_id.



My records will look something like this:



id / name / user_id / group_id



4 / NULL / 125 / 1 -> The id of the Junior group that is in the same table.



5 / NULL / 125 / 3 -> The id of the Senior group that is in the same table.



Is this a valid way to do it? How wrong it is?









share|improve this question
























  • Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

    – apokryfos
    Mar 27 at 8:46











  • This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

    – Kristian Vasilev
    Mar 27 at 8:51

















0















I'm having a many to many relation. Lets say that I have Many users that can be in Many groups and the other way around.



I wan't to make a table that contains the following columns



group_user



id / name / user_id / group_id



Since I have only 3 groups Junior, Middle and Senior I don't want to make another separated table for them. My idea here is to make the following approach:



  1. Creating 3 records in the same table group_user with the following data

id / name / user_id / group_id



1 / Junior / null / null



2 / Middle / null / null



3 / Senior / null / null



So now when I want to insert a group_id I will use this 3 ID's that I just created with NULL user_id and group_id.



My records will look something like this:



id / name / user_id / group_id



4 / NULL / 125 / 1 -> The id of the Junior group that is in the same table.



5 / NULL / 125 / 3 -> The id of the Senior group that is in the same table.



Is this a valid way to do it? How wrong it is?









share|improve this question
























  • Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

    – apokryfos
    Mar 27 at 8:46











  • This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

    – Kristian Vasilev
    Mar 27 at 8:51













0












0








0








I'm having a many to many relation. Lets say that I have Many users that can be in Many groups and the other way around.



I wan't to make a table that contains the following columns



group_user



id / name / user_id / group_id



Since I have only 3 groups Junior, Middle and Senior I don't want to make another separated table for them. My idea here is to make the following approach:



  1. Creating 3 records in the same table group_user with the following data

id / name / user_id / group_id



1 / Junior / null / null



2 / Middle / null / null



3 / Senior / null / null



So now when I want to insert a group_id I will use this 3 ID's that I just created with NULL user_id and group_id.



My records will look something like this:



id / name / user_id / group_id



4 / NULL / 125 / 1 -> The id of the Junior group that is in the same table.



5 / NULL / 125 / 3 -> The id of the Senior group that is in the same table.



Is this a valid way to do it? How wrong it is?









share|improve this question














I'm having a many to many relation. Lets say that I have Many users that can be in Many groups and the other way around.



I wan't to make a table that contains the following columns



group_user



id / name / user_id / group_id



Since I have only 3 groups Junior, Middle and Senior I don't want to make another separated table for them. My idea here is to make the following approach:



  1. Creating 3 records in the same table group_user with the following data

id / name / user_id / group_id



1 / Junior / null / null



2 / Middle / null / null



3 / Senior / null / null



So now when I want to insert a group_id I will use this 3 ID's that I just created with NULL user_id and group_id.



My records will look something like this:



id / name / user_id / group_id



4 / NULL / 125 / 1 -> The id of the Junior group that is in the same table.



5 / NULL / 125 / 3 -> The id of the Senior group that is in the same table.



Is this a valid way to do it? How wrong it is?






php laravel migration table-structure






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 8:27









Kristian VasilevKristian Vasilev

187 bronze badges




187 bronze badges















  • Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

    – apokryfos
    Mar 27 at 8:46











  • This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

    – Kristian Vasilev
    Mar 27 at 8:51

















  • Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

    – apokryfos
    Mar 27 at 8:46











  • This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

    – Kristian Vasilev
    Mar 27 at 8:51
















Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

– apokryfos
Mar 27 at 8:46





Create a separate table for the metadata of the 3 groups because what you are doing here contains inapplicable nulls which are not allowed in a normalised relational database. (Inapplicable NULL is a null that can never have a value under any circumstances)

– apokryfos
Mar 27 at 8:46













This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

– Kristian Vasilev
Mar 27 at 8:51





This makes sense! Thanks a lot I will read more for "Inapplicable NULL", thanks.

– Kristian Vasilev
Mar 27 at 8:51












1 Answer
1






active

oldest

votes


















0














I would recommend following the correct procedures as follows:



Table 1 users table:
id | name



Table 2 groups table:
id | name



Table 3 group_user pivot table:
id | user_id | group_id



Note: Table 3 should never hold a nullable value



The relation would be as follows, right now we have two models, a User model and a Group model.



both will have a belongsToMany relation towards each other.



Here is the method you will use in the User model:



public function groups()

$this->belongsToMany(User::class);



And here is the method you will use in the Group model:



public function users()

$this->belongsToMany(Group::class);



This is the recommended way to continue.






share|improve this answer

























  • I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

    – Kristian Vasilev
    Mar 27 at 9:01











  • no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

    – vahan terzibashian
    Mar 27 at 9:06











  • It works like a charm, thanks!

    – Kristian Vasilev
    Mar 28 at 12:29










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372671%2flaravel-the-right-way-for-many-to-many-table-structure%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I would recommend following the correct procedures as follows:



Table 1 users table:
id | name



Table 2 groups table:
id | name



Table 3 group_user pivot table:
id | user_id | group_id



Note: Table 3 should never hold a nullable value



The relation would be as follows, right now we have two models, a User model and a Group model.



both will have a belongsToMany relation towards each other.



Here is the method you will use in the User model:



public function groups()

$this->belongsToMany(User::class);



And here is the method you will use in the Group model:



public function users()

$this->belongsToMany(Group::class);



This is the recommended way to continue.






share|improve this answer

























  • I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

    – Kristian Vasilev
    Mar 27 at 9:01











  • no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

    – vahan terzibashian
    Mar 27 at 9:06











  • It works like a charm, thanks!

    – Kristian Vasilev
    Mar 28 at 12:29















0














I would recommend following the correct procedures as follows:



Table 1 users table:
id | name



Table 2 groups table:
id | name



Table 3 group_user pivot table:
id | user_id | group_id



Note: Table 3 should never hold a nullable value



The relation would be as follows, right now we have two models, a User model and a Group model.



both will have a belongsToMany relation towards each other.



Here is the method you will use in the User model:



public function groups()

$this->belongsToMany(User::class);



And here is the method you will use in the Group model:



public function users()

$this->belongsToMany(Group::class);



This is the recommended way to continue.






share|improve this answer

























  • I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

    – Kristian Vasilev
    Mar 27 at 9:01











  • no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

    – vahan terzibashian
    Mar 27 at 9:06











  • It works like a charm, thanks!

    – Kristian Vasilev
    Mar 28 at 12:29













0












0








0







I would recommend following the correct procedures as follows:



Table 1 users table:
id | name



Table 2 groups table:
id | name



Table 3 group_user pivot table:
id | user_id | group_id



Note: Table 3 should never hold a nullable value



The relation would be as follows, right now we have two models, a User model and a Group model.



both will have a belongsToMany relation towards each other.



Here is the method you will use in the User model:



public function groups()

$this->belongsToMany(User::class);



And here is the method you will use in the Group model:



public function users()

$this->belongsToMany(Group::class);



This is the recommended way to continue.






share|improve this answer













I would recommend following the correct procedures as follows:



Table 1 users table:
id | name



Table 2 groups table:
id | name



Table 3 group_user pivot table:
id | user_id | group_id



Note: Table 3 should never hold a nullable value



The relation would be as follows, right now we have two models, a User model and a Group model.



both will have a belongsToMany relation towards each other.



Here is the method you will use in the User model:



public function groups()

$this->belongsToMany(User::class);



And here is the method you will use in the Group model:



public function users()

$this->belongsToMany(Group::class);



This is the recommended way to continue.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 8:55









vahan terzibashianvahan terzibashian

983 silver badges7 bronze badges




983 silver badges7 bronze badges















  • I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

    – Kristian Vasilev
    Mar 27 at 9:01











  • no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

    – vahan terzibashian
    Mar 27 at 9:06











  • It works like a charm, thanks!

    – Kristian Vasilev
    Mar 28 at 12:29

















  • I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

    – Kristian Vasilev
    Mar 27 at 9:01











  • no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

    – vahan terzibashian
    Mar 27 at 9:06











  • It works like a charm, thanks!

    – Kristian Vasilev
    Mar 28 at 12:29
















I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

– Kristian Vasilev
Mar 27 at 9:01





I will definitely take this way. I was thinking that It's stupid to have a table with 3 records in it. But it's even worst to have a table with denormalizated data in the table. Thanks a lot for the answer and the spend time to write it :)

– Kristian Vasilev
Mar 27 at 9:01













no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

– vahan terzibashian
Mar 27 at 9:06





no no, it wasn't stupid of you to think of it that way, I know it doesn't make much sense, but you will see that this will flow very naturally inside your application. Update me on how it goes.

– vahan terzibashian
Mar 27 at 9:06













It works like a charm, thanks!

– Kristian Vasilev
Mar 28 at 12:29





It works like a charm, thanks!

– Kristian Vasilev
Mar 28 at 12:29








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372671%2flaravel-the-right-way-for-many-to-many-table-structure%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript