Many to many relation for a user to regions and roles - laravel

In my application there was a many to many relation between a user and regions, and the same for user and roles.
Previously a user had many regions under one city. Also, a user had many roles in one city, primarily Admin as the second role.
Now there's a change in the system. A user can have two roles in multiple city.
For example: User A has 4 regions. 2 regions belong to city X and 2 belong to city Y. And user has two different roles in city X and city Y.
My current schema is below:
User
belongsToMany('App\Region', 'user_region', 'user_id', 'region_id');
belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
I used Entrust in Laravel for Roles and Permission.
How can I implement this new system with my existing schema? v1 is already on production. I need to change this in v2.

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

parse.com - Possible to 'and' multiple ACLs?

Using parse.com.
I have the following format set up:
City roles
Department roles
Users that belong to a specific Department and City combination (i.e. user has relation to Department and City role)
I have a Register class. I want to restrict access to an object in that class to a user that belongs to BOTH Department AND City. Is that possible?
I know I could have a new role for each City+Department combination but I could end up with hundreds of roles. That seems incorrect/inefficient.
Is there a good way to do this?

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