Complicated Eloquent Relationships - laravel

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.

Related

Laravel - Pivot table with 3 models realtionships Many to Many and One to Many

I want to make authentification in my Laravel project for Users who have Roles and Permissions. Actually That's not a problem to do but Users can create Teams or be invited member of any team and each User in any Team has his ONE Role in the team(in another team the user may have another role). In essense Users must have many Roles but only one role in one particular Team and I can't understand if I create correct DB relationships.
Please check the image with relationships to get more info:
In general:
Users have many Teams, and Teams have many Users(Many to Many)
Teams have many Roles, Roles have many Teams (Many to Many)
Users have many Roles, But only one Role in a Team what relationship should be there?
I'm interested how to do that properly. How to minimize messy code in the future and avoid of need to redesign DB relationships.
Thank you guys so much!
Maybe you do need a polymorphic relationship:
https://laravel.com/docs/master/eloquent-relationships#custom-polymorphic-types
with a morph map.
https://nicolaswidart.com/blog/laravel-52-morph-map

Laravel: Table structure for multiple users types, polymorphic relationships

In my site (api using laravel 5.6 and laravel passport) I have two types of users (Teachers and Students), in the future there will be more. The teacher and student entities are very different, meaning that if I keep them all in one table, the table will be long and many fields will have a null value. Right now I have one Users table with common fields and two other tables (Teachers and Students) to which I have setup a polymorphic relationship from user. My question is if this is a good approach, or if there are other ways to handle this more elegantly?
I would create 1 table for Teachers and 1 table for Students and not use the Users table/model. This way you can keep them completely separate and not worry about adding more types of users in the future. Continually trying to fit new users into your existing Users model, which would be shared, is a headache. I made this same mistake when I started and eventually had to rework the project.
There are plenty of guides for Laravel multi-auth / multi-user online.
Here are a couple to help you get started:
https://medium.com/hello-laravel/multiple-authentication-system-laravel-5-4-ac94c759638a
https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt
Also, there are cases where it makes sense to use the User model for multiple types of users. For example, you may have multiple roles for a user where most/all of the fields are the same (not your scenario). In this case, you can assign a 'role' to each User and the check the roles for actions (e.g. add middleware to prevent roles from accessing various routes). Here is an example:
https://medium.com/#ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a
Since you said the teacher and student entities are very different, you should keep them separate.

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');
}

Database schema for kind of Hospital Management in 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

laravel relationships - 3 way pivot - eloquent

I am following a tutorial on Laravel ACL, that takes into consideration Users and Roles (https://github.com/laracasts/laravel-5-roles-and-permissions-demo). However, I have an additional level of complexity whereby I need to take into consideration a company model also. So a user may belong to many companies and each instance of a company user may have many roles.
I think the best way to acheive this would use a pivot that has 3 fields:
company_id
role_id
user_id
Esentially, I want to achive something like this:
$user->companyRoles; //return the user's company roles
$user->company->assignRole('admin');
$user->assignRole($companyId, 'admin');
Can you advise on the relationships I require to do this?
Many thanks

Resources