Database schema for kind of Hospital Management in Laravel - laravel

I am trying to start an application but it's not for managing hospital, it will be for doctors and patients. Where there will be multiple types for users will be able to login such as Doctor, Patients/Guardians.
Doctors can have multiple clinics at multiple locations and doctor can manage patient records. Once the patient account has been created by doctor then patient can take appointment from doctor or update his appointment status and many more stuff will be there next.
The thing is how to go with the ERD?
I will have
User //User accounts used to login in to the system
Doctor
Patient
Guardian
Role
Permission
These are the models I have currently created, but they don't seem right to me.
Should I remove role columns as I already have different tables for different pre-defined roles?
Or should it be there? But how to manage permissions on users if no roles table is there?
Also, most importantly, how to go with one to one with users? I mean should I go and create functions in user model such as:
public function doctor(){}
public function guardian(){}
public function patient(){}
Or is there a better approach to follow?

If they are all users, you can extend different users from a base user model.
If they require different columns in database, consider single table inheritance.
If roles are static, I would create a class called UserType and have constants of each user type mapped to an integer. In the database, the user table will have a type column which is mapped to this integer.
For example:
class UserType {
const DOCTOR = 1;
}
In your application you'll be able to check the type of user by doing $user->type === UserType::DOCTOR
In the Eloquent itself, you can extend newFromBuilder method to check the type attribute and return the child class (like Doctor) instead of User. So even when you do $user = User::find(1);, you'll still get the class Doctor.
When creating users, you can just create Doctor itself but make sure in __construct to set the appropriate type attribute.
So now you have a base User class, your shared functionality can go here. Specialised methods such as relation to clinics can go in the Doctor class.
This is somewhat similar to the above: https://github.com/Nanigans/single-table-inheritance

Related

How to get specific model profile after authorization?

Here are some user profiles like: DoctorModel, UserModel, ClinicModel.
Each has own set of fields in database.
How to add concrete model in global scope when user is authorized to be able get model fields across all application.
For exmaple if user authorized as clinic I want to get from this model field nameClinic everywhere.
Now by defaul I got UserModel form Auth::user()
IMO this is somewhat a wrong approach. You can maintain a UserModel for all types of Users and the details that might change can be held on other Models.
For example, the ClinicModel belongsTo relationship on UserModel and holds the details specific to the clinic over there.

One model, one table different user types and different controllers, how to handle policies?

I have one model user and one table against that model users I have different roles for user as patient, hospital and so on, each user have different fields and values to enter during registration so I have created different routes and controllers for different roles.
Now I'm facing problem during authorization process how I can authorize. I have only one model user so I can create only one policy called UserPolicy and use the $this->authorize method in the UserController.
I have also other controllers as PatientController, HospitalController which all bound to the one table and model called user and fetching the record only based on the user type. Now how can I create the policies for them and use the $this->authorize method in the Hospital, Patient controllers?
you are using the same table and you require different data from each kind if user ?
anyway if that the situation , you can create a type field in the users table , then create 2 middlewares , in your middleware check the type of the user then throw an exception or make him pass

Laravel Relationships and Pivot

I have a users table
id
role_id
"other standard auth stuff"
I also have a roles table
id
role
I do not want to change the users table in the sense that its currently doing all the auth stuff provided with laravel 5.4 but
I have users with a role. Let's say coach and player are 2 types of roles and I need to now allow coaches to add players and players to add themselves to coaches and have them related.
Is it possible to relate the User model role type coach and role type player in a separate table like coaches_players without creating new models for Player and Coach? If so can someone point me in the right direction. I know if I were to start from scratch I would have Player and Coach related through belongsToMany etc. but everything in my system works off of user_id and I do not want to change all the Auth functionality etc.
Is there a way to create those Player and Coach models that extend User and create the relationships through those?
Is there a way to relate 2 columns in the User table through a pivot table coaches_players?
Just looking for some guidance and possibly some links if you have them off the top of your head.
Sure, you can do this. Just create coaches_players table and define new relationships in User model:
public function players()
{
$this->belongsToMany(User::class, 'coaches_players', 'coach_id', 'player_id');
}
public function coaches()
{
$this->belongsToMany(User::class, 'coaches_players', 'player_id', 'coach_id');
}

Three way relationship in Laravel

having a brain failure with a relationship between three objects, hoping someone can help me out.
I have four models: Team, User, ProjectType and Project
Team has many User, has many ProjectType
User belongs to many Team, has many ProjectType
ProjectType belongs to manyUser, belongs to many Team, has many Project
Project belongs to ProjectType
As a single user can belong to many teams, I want to request the ProjectTypes that a User has access to, but only within the Team they are currently logged in with. It may be the case that a User has access to project types across multiple teams, but will only be logged in to one team at any time, so I need just that subset.
I'm hoping this structure makes sense, but I'm struggling to get access to the data I want easily
So I'd like to do $user->projectTypes and get all project types for that user, but only the subset of the team they're currently logged in with.
Equally, once I've got that, I want to be able to get $user->projectTypes->projects within that set.
I'd like to do this whilst maintaining all of the nice relationship methods I get with Laravel, but am struggling to setup the data structure to support this, and get the data in turn.
Worth adding I'm using Laravel 4.2, but am not desparately tied to it, and can upgrade to 5.x if necessary to get this functionality.
Once you've defined the relationships as you've described, you can access the ProjectTypes that belong to a User, that also belong to a certain Team (in your case $teamid should be the id of the Team that the User is currently logged in to) like so:
$projectTypes = $user->projectTypes()->where('team_id', $teamid)->get();
To easily access a collection of all Projects that belong to all ProjectTypes that belong to a User, you would first define the HasManyThrough relationship like so:
class User extends Eloquent {
public function projects()
{
return $this->hasManyThrough('Project', 'ProjectType');
}
}
Then you can access that collection like so:
$projects = $user->projects;
And finally, to access the Projects that belong to the ProjectTypes that belong to a User, that also belong to a certain Team (i.e. what it seems you're looking for), you can use lists() to get a list of relevant ProjectType ids, then whereIn() to filter for those within that list:
$projectTypeIds = $user->projectTypes()->where('team_id', $teamid)->lists('id');
$projects = $user->projects()->whereIn('projecttype_id', $projectTypeIds)->get();

Complicated Eloquent Relationships

I have a many-to-many relationship, users and roles, which I have worked out just fine. However, I have another table I would like to relate as well, called leads. A user and that user's role can be assigned to a lead.
So for example, I've created a Role and let's call it 'Manager'. Now, when I am managing my Users, I'd need to be able to assign different users to different roles which will be a many-to-many relationship (a role can have many users, a user can have many roles). So I will assign the role of 'Manager' to User A.
Now, when I am modifying my leads, I'd like to be able to assign a role_user to my lead (In my example, I'd like to assign a user to that lead), but I'd first need to assign a role to that Lead (Manager), and then be able to assign a user that is of that role to that lead.
Currently, I've got a many-to-many relationship setup for users and roles, using the pivot table name role_user. I've then got a many-to-many relationship setup on that pivot table, role_user, and leads, using another pivot table named lead_role_user.
Models + Controller - http://paste.laravel.com/D6h
My error: Call to undefined method Illuminate\Database\Query\Builder::roles()
It feels as though I am making this much more difficult than it should be.

Resources