Published 1 week ago by davfg
Hello, I have a problem when designing my models related to users and their relationships. In my application, I have three types of users: Admin, Manager and team of competitors. The models that I have thought to create so far are:
It should be mentioned that the attribute "competition_id" in the user table refers to a team of competitors participating in a certain competition, a manager can configure a single competition and an administrator can configure all competitions (set to null for admin) .
My problem arises when I want to relate these models to each other, where I have read that it can be done with polymorphic relationships, but I am not sure about it. I would appreciate any help regarding a new way of designing these models and their relationships or how to design relationships between these models.
Thank you very much for your time.
@davfg A competitor is just a user. Don’t create a separate model for them. It’s just going to cause headaches when it comes to authentication having multiple user models.
Instead, use relationships to determine if a user is a “competitor”. Create a many-to-many relationship between users and teams.
Also, keep your naming consistent. You use snake_case (as you should for most column names) but then you have “educationalDegree” camelCased. Stick with a common naming convention (snake_case if you want Laravel’s “magic” to work when it comes to models and relations).
Finally, use authorization to determine what each of your user types can do.
Thank you very much @martinbean for your response. As I understood, you recommend me to create a single user model that has all the attributes of User and Competitor, but I do not understand the relationship of many to many with Team, since only competitors belong to a team, while Managers and Administrators do not. And it would not be bad practice to establish many columns in User that only competitors will use, being the value of these columns for managers and administrators null?