0

I have a Laravel project that currently has users who are all Doctors. I would like other types of user like Teachers, Policemen, etc. to use the site. This requires me to have different roles, which is not an issue. The issue is each type of role requires different user information in the Users table. (e.g for Doctors I need to save their qualifications, speciality, etc while for Policemen I want to save their years of service, rank, title, etc.).

All these people need to use the Users table so that they are able to login. I would like to know what is the best way to achieve this.

What I have tried:

  • One Users table with basic fields (name, email, address, phone), then created additional tables with the additional fields for each user type (i.e. created Doctors table with 'speciality',
    'qualifications', etc. fields) and then linked each Doctors instance to a User using user_id. Same thing for Teachers, Policemen, etc. The problem here is that I cannot get all the information I require of the user once their logged in. (i.e If I need to pull the years of service for the person who is logged in, I can't (or can I?) because that information is in a different table.
  • Multiple tables (Policemen table, Doctors table, etc). With this, I can get the tables to have all the information I need but the issue with this is I cant use all the different tables to log the users in (or can I?)
  • One User table with all fields for all types of users. User registers with email, password and role and then fills in the fields relevant to them once their role has been determined. The issue with this is that the Users table then has too many columns.

I may be missing something very obvious but someone please help me with best practice here or what is the most logical approach?

1
  • 1
    As I see it, a User is only identified by login credentials. Additional credentials, such as types, names, addresses etc., should be a relation. Commented Sep 18, 2020 at 13:25

2 Answers 2

3

What you need is 3 tables to do it without duplication.

  1. A users table for all the shared information.
  2. A details table(name it whatever you want) that has user_id, attribute, value
  3. A Roles table which basically lists all the available roles.

The rest is relationships:

  • A usual relation between users and roles, docs
  • A one to many relationship between users and details docs

Then you can check if the user is a specific role, and retrieve the data associated with that role.

Sign up to request clarification or add additional context in comments.

Comments

2

Add Relationship Between These Tables

Users -id, name, ...

Doctors -id, qualifications, specialty, user_id (FK)

Policemen -id, rank, title, user_id (FK)

And When you registering doctor or policeman add data in two tables with the help of transactions so basic login information of user save in users table and other information store specific doctor or policeman table and you can easily get these all data with the help of laravel relationship.

2 Comments

There is a problem in using this approach, imagine if the amount of type (or role) of the users using this site is something like 100, then you'll need a 100 tables. But if every role and their details are decoupled then you'll have something like a details table, a roles table and a users table, inserting all the details for each role in one table, and every roles in another while using the users table completely alone.
@BillionShiferaw I think this solution is much better than the one you're offering. The key-value paradigm may be practical to store data easily, but it's complexity for nothing. If he has different user types, he will have process for every one of them. So a model for each seems logical, and then business entities should handle their own business logic. Forms and display pages need to be adapted for each user type. Your solution is good only for relatively rare cases, where we're looking to provide a tool with lots of flexibility (a CMS, basically, where admins can add user types and so...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.