PHPackages                             mdnayeemsarker/role-permission - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. mdnayeemsarker/role-permission

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

mdnayeemsarker/role-permission
==============================

A Laravel package for role-based permissions with section permissions.

1.0.0(1y ago)18MITPHPPHP &gt;=8.1

Since Nov 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mdnayeemsarker/RolePermission)[ Packagist](https://packagist.org/packages/mdnayeemsarker/role-permission)[ RSS](/packages/mdnayeemsarker-role-permission/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Laravel RolePermission Package
==============================

[](#laravel-rolepermission-package)

This package provides RolePermission for Laravel applications.

Installation
------------

[](#installation)

To install the package, use Composer:

```
composer require mdnayeemsarker/role-permission
```

Service Provider Registration
-----------------------------

[](#service-provider-registration)

In your Laravel project’s config/app.php, add the RolePermissionServiceProvider to the providers array if it’s not already there:

```
'providers' => [
    // Other service providers...
    Mdnayeemsarker\RolePermission\MnsRpServiceProvider::class,
],
```

Publishes Method
----------------

[](#publishes-method)

```
php artisan vendor:publish --provider="Mdnayeemsarker\RolePermission\MnsRpServiceProvider"
```

Clear Cache &amp; Autoload Files Again
--------------------------------------

[](#clear-cache--autoload-files-again)

```
php artisan config:clear
php artisan cache:clear
composer dump-autoload
```

Migrate
-------

[](#migrate)

```
php artisan migrate
```

For testing make prepare db seeder or input some dummey data.
-------------------------------------------------------------

[](#for-testing-make-prepare-db-seeder-or-input-some-dummey-data)

- for db seeder make RolePermissionsTableSeeder

```
public function run(): void
{
    // Create some dummy roles
    Role::create(['name' => 'admin']);
    Role::create(['name' => 'editor']);
    Role::create(['name' => 'user']);

    // Create some dummy permissions
    Permission::create(['name' => 'view-dashboard', 'section' => 'admin']);
    Permission::create(['name' => 'edit-articles', 'section' => 'editor']);
    Permission::create(['name' => 'view-articles', 'section' => 'user']);
    Permission::create(['name' => 'delete-articles', 'section' => 'admin']);

    // Create some dummy role permission
    RolePermission::create(['role_id' => '1', 'permission_id' => '1']);
    RolePermission::create(['role_id' => '1', 'permission_id' => '4']);
}
```

Prepare DatabaseSeeder.php file (databse/seeders)
-------------------------------------------------

[](#prepare-databaseseederphp-file-databseseeders)

```
public function run(): void
{
    // another code
    $this->call([
        // another seeder
        RolePermissionsTableSeeder::class,
    ]);
}
```

Run DB Seeder command
---------------------

[](#run-db-seeder-command)

```
php artisan db:seed
```

Update User.php in (App\\Models)
--------------------------------

[](#update-userphp-in-appmodels)

```
// Define the relationship with Role (Override)
public function role()
{
    return $this->belongsToMany(Role::class, 'user_roles');
}
// Define the relationship with Permissions
public function allPermissions()
{
    return $this->role->flatMap->permissions;
}
```

Assign user role
----------------

[](#assign-user-role)

Go to your controller

```
$user = Auth::user();
$role = Role::find(1); // Find the role you want to assign
$user->role()->attach($role); // Assign the role
```

Check user all permission
-------------------------

[](#check-user-all-permission)

```
$user = Auth::user();
if ($user && $user->role()->exists()) {
    dd($user->allPermissions()); // this retuens user all permission
} else {
    dd('User has not any permission'); // set some permission accrose user and check
}
```

Check User all permission with permission name
----------------------------------------------

[](#check-user-all-permission-with-permission-name)

```
$user = Auth::user();
if ($user && $user->role()->exists()) {
    $permissions = $user->allPermissions();
    $permissionNames = $permissions->pluck('name');
    dd([$permissions, $permissionNames]); // Debugging
} else {
    $permissions = [];
    dd($permissions); // Debugging
}
```

Check permission in contoller
-----------------------------

[](#check-permission-in-contoller)

- Fist make and Helper (app/Helper/PermissionHelper.php)

```
role()->exists()) {
            $permissions = $user->allPermissions();
            return $permissions->contains('name', $permissionName);
        }
        return false;
    }
}
```

- Controller check permission

```
if (PermissionHelper::hasPermission('delete-articles')) {
    dd('Permission granted: OK');
}else{
    dd('Permission not granted: NOT');
}
```

For check permission in blade
-----------------------------

[](#for-check-permission-in-blade)

- First create an Helper.php (app/Http/Helper.php) and set composer.json autoload

```
"autoload": {
    "files": [
        "app/Http/Helper.php"
    ]
}
```

- Helper function code

```
if (!function_exists('hasPermission')) {
   /**
     * Check if the authenticated user has a specific permission.
     *
     * @param  string  $permission
     * @return bool
     */
    function hasPermission($permissionName)
    {
        $user = Auth::user();
        if ($user && $user->role()->exists()) {
            $permissions = $user->allPermissions();
            return $permissions->contains('name', $permissionName);
        }
        return false;
    }
}
```

- blade file code

```
@if (hasPermission('delete-articles'))
    Permission granted: OK
@else
    Permission not granted: NOT
@endif
```

Getting To Know MD NAYEEM SARKER
--------------------------------

[](#getting-to-know-md-nayeem-sarker)

- Feel free to [learn more about MD NAYEEM SARKER](https://github.com/mdnayeemsarker).

License
-------

[](#license)

MIT © [MD NAYEEM SARKER](https://github.com/mdnayeemsarker)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

529d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed641d9eb4ce01e8006974fac6a26da638cd3ddb5e01ff9e60eb5284feedf539?d=identicon)[devabnayeem](/maintainers/devabnayeem)

---

Top Contributors

[![mdnayeemsarker](https://avatars.githubusercontent.com/u/34866701?v=4)](https://github.com/mdnayeemsarker "mdnayeemsarker (1 commits)")

### Embed Badge

![Health badge](/badges/mdnayeemsarker-role-permission/health.svg)

```
[![Health](https://phpackages.com/badges/mdnayeemsarker-role-permission/health.svg)](https://phpackages.com/packages/mdnayeemsarker-role-permission)
```

###  Alternatives

[laragear/two-factor

On-premises 2FA Authentication for out-of-the-box.

339785.3k8](/packages/laragear-two-factor)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[yajra/laravel-acl

Laravel ACL is a simple role, permission ACL for Laravel Framework.

112103.9k1](/packages/yajra-laravel-acl)[scaler-tech/laravel-saml2

SAML2 Service Provider integration for Laravel applications, based on OneLogin toolkit

2737.5k](/packages/scaler-tech-laravel-saml2)[alajusticia/laravel-logins

Session management in Laravel apps, user notifications on new access, support for multiple separate remember tokens, IP geolocation, User-Agent parser

2011.0k](/packages/alajusticia-laravel-logins)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
