I am struggeling with the database setup of a membership site - I'd like to hear your idea on how you would approach it.
The core of the challenge is a threefold relation between users - roles and (company) accounts:
Here are the details in words:
Any given USER can have one or more ROLES on one or more ACCOUNTS.
You can picture an ACCOUNT as a company account where different people have access to under different roles.
A user can also have access to different company accounts under different roles (e.g. John can have role of 'account_owner' on Acme account AND have a 'contributor'-role on AMCE2 account )
To me this seems like a 'many-to-many-to-many' relationship.
Here are the details in code:
USERS have personal user accounts (containing a person's login credentials etc.) so there is a users table.
// User Accounts Table
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); // John Doe, Jane Doe, ...
// user credentials, email, password, ...
$table->timestamps();
});
There is a list of given ROLES, so there is a roles table:
// Roles table
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); // account_owner, contributor, ...
// permissions, ...
$table->timestamps();
});
And lastly I would believe there needs to be an accounts table:
// Company Accounts Table
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); // Acme Ltd., Company ABC, ...
// company information, account type, ...
$table->timestamps();
});
So in a typical many-to-many setup i could only connect 2 of these tables. This is where I get stuck on what would be the best way to go about it.
- just add a 3rd column to the pivot table making it a "threefold pivot table" (user_id, role_id and account_id)?
- adding 3 pivot tables to connect 2 tables each?
I am curious on what you think!