Laravel - 建模表关系的最佳策略

Laravel - Best strategy to model tables relations

我有以下问题。

我正在尝试找出组织用户-角色-商店-学校之间关系的最佳方式。

一个用户可以将多个角色附加到一家商店或一所​​学校,因此可以创建现实:

"User [testuser@email.com] is [root] for school [HighSchool1]"

还有

"User [testuser@email.com] is [user] for store [Store1]"

因此一个用户可以有多个角色,但该角色是附属于学校或商店的

一开始我正要这样做...

``` 用户 -ID -姓名 -电子邮件 -密码

Roles
-id
-name

Roles_user
-id
-rol_id
-user_id
-school_id           //whatch this
-store_id             //whatch this

School
-id
-name

Store
-id
-name

问题是,将来系统还可以处理超市......我会更改我的 roles_user table 以附加新的列。

我怎样才能实现这个....使用 corse Eloquent 和关系?

取决于你想要达到的目的。我会建议你 business_typebusiness_id 例如

Roles_user
-id
-rol_id
-user_id
-business_type           //whatch this
-business_id             //whatch this

Business_type 可以获取学校、商店、超市、酒店等值,或来自 table 个注册企业的 ID。

这完全取决于您想要实现的目标。希望对你有帮助