Hey there im wondering what is the best option for setting up my database.
I need to store a user that belongsToMany companies.
The user also belongsToMany stores.
And at last the user need to be stored to specific stores that are based on the selected stores.
I was thinking of setting up these pivot tables.
company_user
user_id
company_id
store_user
user_id
store_id
company_store_shop
user_id
store_id
shop_id
Is this a good approach or does any of you have a better solution.
And how do i store this? i have tried this using sync but that only seems to be working for two way pivot tables and not three way pivot tables.
Hey @bestmomo thanks for your reply. Im not a to big fan for using a package for this. somehow it limits my knowledge solving this problem.
I might look into polymorphic but im not sure that is the way to go for this :)
Need to say my experience with polymorphic relations is zero but if there is no better option i will look into it!
@bestmomo I might ask to much from you but can you help me set up the database structure with polymorphic relation and explain a little bit of the structure.
That would help me so much and maybe others who have the same question as me :)
companies
id - integer
...
stores
id - integer
...
users
id - integer
...
userables
user_id - integer
userable_id - integer
userable_type - string
Store (same for Company) :
class Store extends Model {
...
public function users()
{
return $this->morphToMany('App\User', 'userable');
}
}
User :
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
...
public function stores()
{
return $this->morphedByMany('App\Store', 'userable');
}
public function companies()
{
return $this->morphedByMany('App\Company', 'userable');
}
}
@bestmomo Thank you for explaining! Though, i don't quite understand what "userables" is being used for? What does it do (userable_id and userable_type)?
Secondly, am i correct that in this setup, the user is connected to the company and the store, but the store not connected to the company? Or is that being undone with the userables table?
@bestmomo Im wondering how i can store this i understand the logic now already a lot better.
When i create a user and save the companies, stores and shops arrays into the userables table how can i do that?
I now have
$user = User::create($request->except('company_list', 'stores', 'shops');
i guess i then need to do something like: $user->companies()->create('??');
Only one problem is that when i want to save my shops im getting this array back (the id's of shops):
array:5 [▼
0 => "1"
1 => "2"
2 => "1"
3 => "2"
4 => "1"
]
And in my userables table it saves it as 2 rows with each the id's (1 and 2).
My problem is that there is no connection between the store and the shop. (Since each store can have the same shops).
So i don't understand how i can create the connection from the shop id to the corresponding store id, in the userables table.
there is a relation i guess, im creating an admin panel to save a user, i choose to which companies the user belongs to and the stores that belongs to the given companies. when i have done that i choose the shops they have acces to with the given stores.
Then i create a store and bind it to a single company.
Then i create a shop
After creating shops i can bind the shops to the store.
Now when i create a user and choose multiple companies it shows the stores that belongs to the companies.
Now we need to choose from the store the shops that belongs to the stores. (remember the modules are build up thanks to step 4)
You need to see it like this: there is a big company that holds multiple stores and those stores have multiple little shops.
When saving a user i need to see to which companies they belong as well to which stores an shops.
Well the user that is created logs in and create new users but can only choose to the store and shops he self has acces to.
I am at master level and can choose of all companies and stores etc.
When the created user logs in he has admin rights and can only create users that are bind to the same companies and stores he has acces to.
I know it sounds confusing, maybe it helps if you thinks of it like this: store = projects and shops = modules.
I think that makes more sense but its not what my project is about ;)
Yes exactly, i have tried to get it working in the meantime but somehow i need to figure out a good way to get the shops unique or combined to the store when sending the form. otherwise it wont know when sending the form what shop id belongs to what store. Any suggestions for that? or maybe an other workaround to achieve this.
I was thinking the same!
One thing i don't really like is the auto increment id in the shop_store table.
I think that id can build up quick not sure if thats a really big problem but just a concern.
So then i could use that auto increment id from the shop_store table into the userables table right? :)
If you do that you dont need the userables table, but only the shop_store_user pivot, because you just relate users to couples store/shop. So for each user you can know : stores, shops, and companies (with stores).