Laravel Relationships and Pivot - laravel

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

Related

User ownership of table that can reference multiple users

I have a small application which has 2 migrations, team and team_user (it also has the default auth migrations).
The teams & default user migration has a belongsToMany relationship. As of now, everyone has the same relationship to each team. What would be a good elegant way to create ownership to the team as a sort of Team Leader. I'm currently exploring creating an additional team_leader migration but not sure if that's the correct solution.
If a team only has one lead you could add a team_lead_id column on the teams table that is a foreign key reference to the team lead user. Then you could define the relationship on the Team model:
public function leader()
{
return $this->belongsTo(User::class);
}
In the case of multiple leads per team then you'd need a pivot table like the team_leader table you mentioned.

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

Laravel - What's the right relationship?

I have users table. (User Model & Controller)
each user can start a fight. the fight table contains the user_id. (I already have Fight Model & Controller)
Once the fight has finished, the record is deleted from the fight table.
The question:
What's the right relationship between users and fight so I can access the user fight within user->fight?
How I can check using Laravel to make sure the user has no fight in the fight table before allowing him to create a new one? (SELECT * FROM fight WHERE user_id = USER_ID)
The correct relationship on the user side is:
$this->hasMany(Flight::class);
Also about your second question, there is an 'has' method that can query a relationship for existance.
So in your case:
$user->has('flight');
Reference: https://laravel.com/docs/5.2/eloquent-relationships#querying-relations

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