Zubair
Zubair

Reputation: 53

Get User Permissions List

I'm Trying To Get all Permissions User has..

Permission Model

public function roles()
{
    return $this->belongsToMany("App\Models\Role");
}

Role Model

public function users()
{
    return $this->belongsToMany('App\Models\User');
}

public function permissions()
{
    return $this->belongsToMany("App\Models\Permission");
}

User Model

public function roles()
{
    return $this->belongsToMany('App\Models\Role', 'role_user', 'user_id', 'role_id');
}

I Want all Permissions that user has.

Can user has permission viva user Permissions

Upvotes: 2

Views: 1500

Answers (5)

Marcelo Pereira
Marcelo Pereira

Reputation: 61

You can use "with("permission")" in the relationship between user and roles: public function roles() { return $this->belongsToMany('App\Models\Role')->with("permissions"); }

Upvotes: 0

Zubair
Zubair

Reputation: 53

Working Fine

$users = User::with(['roles.permissions'])->get()->find(2);

OutPut

 {
        "status": false,
        "response": {
            "errors": {
                "id": 2,
                "vender_id": 1,
                "name": "zubair",
                "email": "[email protected]",
                "email_verified_at": null,
                "created_at": "2019-06-17 08:11:25",
                "updated_at": "2019-06-17 08:11:25",
                "roles": [
                    {
                        "id": 1,
                        "vender_id": 1,
                        "name": "product-manager",
                        "display_name": "Product Manager",
                        "description": "Product Manager",
                        "created_at": "2019-06-17 08:11:56",
                        "updated_at": "2019-06-17 08:11:56",
                        "pivot": {
                            "user_id": 2,
                            "role_id": 1
                        },
                        "permissions": [
                            {
                                "id": 1,
                                "name": "add-role",
                                "display_name": "Add Role",
                                "description": "Add Role",
                                "created_at": "2019-06-17 08:20:09",
                                "updated_at": "2019-06-17 08:20:09",
                                "pivot": {
                                    "role_id": 1,
                                    "permission_id": 1
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }

But When i Try to Access permissions Property [permissions] does not exist on this collection instance. in Collection.php

Upvotes: 1

mare96
mare96

Reputation: 3859

You need to write something like this in your controller.

$users = User::with(['roles', 'roles.permissions', 'permissions'])->get();

That will return all permissions from roles and permissions.

View:

foreach($user->roles as $role) {
       foreach($role->permissions as $permission){
          {{ $permission->name }}
       }
    }

Upvotes: 3

Neel Bhanushali
Neel Bhanushali

Reputation: 782

Lets say that your hierarchy is User hasMany Role and Role hasMany Permission.

Then your relationships should be set like this in the models.

User Model

public function roles() {
   return $this->hasMany('App\Models\Role`);
}

Role Model

public function permissions() {
   return $this->hasMany('App\Models\Permission`);
}

To get Permissions for the user

User::find(1)->load('roles.permissions')

Upvotes: 0

Patrick Schocke
Patrick Schocke

Reputation: 1491

to get all the permissions a user has, you can simply run

$users = User::with(['roles.permissions'])->get();

To then use the permissions, you need to iterate over the roles like this:

foreach($user->roles as $role) {
   var_dump($role->permission);
}

Upvotes: 0

Related Questions