Search code examples
laravellaravel-7eloquent-relationshiplaratrust

Laratrust show user role inside of team


I am trying to make a connection between teams and users. Pretty much Latrust looks like an awesome package BUT some things I believe could be explained better.

Let's pick a team (say: team_id = 1): $team = Team::where('id', $request->team_id)->first();

And let's pick our users from the team:

$roles = Role::get();
$users = User::whereRoleIs($roles->pluck('name')->toArray(), $team)->get()

This will result in the following:

{
    "message": "ok",
    "data": [
        {
            "id": 1,
            "name": "niki",
            "email": "[email protected]",
            "email_verified_at": null,
            "created_at": "2021-07-12T11:29:09.000000Z",
            "updated_at": "2021-07-12T11:29:09.000000Z",
            "roles": [
                {
                    "id": 3,
                    "name": "user",
                    "display_name": "User",
                    "description": "User",
                    "created_at": "2021-07-12T11:29:08.000000Z",
                    "updated_at": "2021-07-12T11:29:08.000000Z",
                    "pivot": {
                        "user_id": 1,
                        "role_id": 3,
                        "user_type": "App\\Models\\User"
                    }
                },
                {
                    "id": 4,
                    "name": "leader",
                    "display_name": "Leader",
                    "description": "Leader",
                    "created_at": "2021-07-12T11:29:08.000000Z",
                    "updated_at": "2021-07-12T11:29:08.000000Z",
                    "pivot": {
                        "user_id": 1,
                        "role_id": 4,
                        "user_type": "App\\Models\\User"
                    }
                }
            ]
        },
        {
            "id": 2,
            "name": "konna",
            "email": "[email protected]",
            "email_verified_at": null,
            "created_at": "2021-07-12T11:29:09.000000Z",
            "updated_at": "2021-07-12T11:29:09.000000Z",
            "roles": [
                {
                    "id": 3,
                    "name": "user",
                    "display_name": "User",
                    "description": "User",
                    "created_at": "2021-07-12T11:29:08.000000Z",
                    "updated_at": "2021-07-12T11:29:08.000000Z",
                    "pivot": {
                        "user_id": 2,
                        "role_id": 3,
                        "user_type": "App\\Models\\User"
                    }
                },
                {
                    "id": 3,
                    "name": "user",
                    "display_name": "User",
                    "description": "User",
                    "created_at": "2021-07-12T11:29:08.000000Z",
                    "updated_at": "2021-07-12T11:29:08.000000Z",
                    "pivot": {
                        "user_id": 2,
                        "role_id": 3,
                        "user_type": "App\\Models\\User"
                    }
                }
            ]
        }
    ]
}

This returns all the roles those two users have (both of them belong in the same team)

How can we pick ONLY the role that the user has inside the team?

I thought of the following: Getting all the user records with the $users query and then picking the role of the user from there.

 foreach ($users as $user){
        $userArray[] = array_push($userArray, $user->name, $user->roles->first());
    }

However, this will return the first() or last() role attached to the user which can be anything (especially when a user may belong to multiple teams with a different role attached to every team he attends).

So my question is, how can I show the role of a user inside a team?

I was thinking of:

$user->roles->where($team->id)

But this returns all the roles that are attached to the user and not the specific role that the user has on the team.

Any help is appreciated.


Solution

  • Thank you @mrhn for your valuable input

    My solution came in the following form:

    $rolesInTeam[] = null;
    $users = User::whereRoleIs($roles->pluck('name')->toArray(), $team)->get();
    
    foreach ($users as $user){
    $rolesInTeam[] = array_push($rolesInTeam,$user->roles()->wherePivotIn(Config::get('laratrust.foreign_keys.team'), $team)->get());
    }
    

    Which returns as a result:

        null,
        [
            {
                "id": 4,
                "name": "leader",
                "display_name": "Leader",
                "description": "Leader",
                "created_at": "2021-07-12T11:29:08.000000Z",
                "updated_at": "2021-07-12T11:29:08.000000Z",
                "pivot": {
                    "user_id": 1,
                    "role_id": 4,
                    "user_type": "App\\Models\\User"
                }
            }
        ],
        2,
        [
            {
                "id": 3,
                "name": "user",
                "display_name": "User",
                "description": "User",
                "created_at": "2021-07-12T11:29:08.000000Z",
                "updated_at": "2021-07-12T11:29:08.000000Z",
                "pivot": {
                    "user_id": 2,
                    "role_id": 3,
                    "user_type": "App\\Models\\User"
                }
            }
        ],
        4,
        [
            {
                "id": 3,
                "name": "user",
                "display_name": "User",
                "description": "User",
                "created_at": "2021-07-12T11:29:08.000000Z",
                "updated_at": "2021-07-12T11:29:08.000000Z",
                "pivot": {
                    "user_id": 5,
                    "role_id": 3,
                    "user_type": "App\\Models\\User"
                }
            }
        ],
        6```