Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laratrust Laravel Package

santigarcor/laratrust

Laratrust adds role and permission management to Laravel with support for multiple user models, teams, guards, caching, events, middleware, gates/policies, and an optional admin panel for managing roles and permissions.

View on GitHub
Deep Wiki
Context7

sidebarDepth: 2

Querying Relations

The LaratrustUserTrait has the roles and permissions relationship, that return a MorphToMany relationships.

The roles relationship has all the roles attached to the user.

The permissions relationship has all the direct permissions attached to the user.

All Permissions

If you want to retrieve all the user permissions, you can use the allPermissions method. It returns a unified collection with all the permissions related to the user (via the roles and permissions relationships).

dump($user->allPermissions());
/*
    Illuminate\Database\Eloquent\Collection {#646
    #items: array:2 [
    0 => App\Permission {#662
        ...
        #attributes: array:6 [
        "id" => "1"
        "name" => "edit-users"
        "display_name" => "Edit Users"
        "description" => null
        "created_at" => "2017-06-19 04:58:30"
        "updated_at" => "2017-06-19 04:58:30"
        ]
        ...
    }
    1 => App\Permission {#667
        ...
        #attributes: array:6 [
        "id" => "2"
        "name" => "manage-users"
        "display_name" => "Manage Users"
        "description" => null
        "created_at" => "2017-06-19 04:58:30"
        "updated_at" => "2017-06-19 04:58:30"
        ]
        ...
    }
    ]
}
*/

By Role

To retrieve the users that have some role you can use the query scope whereRoleIs or orWhereRoleIs:

// This will return the users with 'admin' or 'regular-user' role.
$users = User::whereRoleIs('admin')->orWhereRoleIs('regular-user')->get();

To get all the users with a set of roles, you can pass an array to the scope:

// This acts as a whereIn check in the database.
$users = User::whereRoleIs(['admin', 'regular-user'])->get();

By Permissions

To retrieve the users that have some permission you can use the query scope wherePermissionIs or orWherePermissionIs:

// This will return the users with 'edit-user' or 'create-user' permission.
$users = User::wherePermissionIs('edit-user')->orWherePermissionIs('create-user')->get();

To get all the users with a set of permissions, you can pass an array to the scope:

// This acts as a whereIn check in the database.
$users = User::wherePermissionIs(['edit-user', 'create-user'])->get();

Roles & Permissions Absence

To retrive all the users that don't have any roles or permissions you can use:

User::whereDoesntHaveRole()->get();

User::whereDoesntHavePermission()->get();
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport