PHPackages                             robypz/laravel-mongo-roles - 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. robypz/laravel-mongo-roles

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

robypz/laravel-mongo-roles
==========================

Simple roles and permission for Laravel using MongoDB.

2.0.0(8mo ago)510MITPHPPHP ^8.2

Since Jul 28Pushed 8mo agoCompare

[ Source](https://github.com/robypz/laravel-mongo-roles)[ Packagist](https://packagist.org/packages/robypz/laravel-mongo-roles)[ RSS](/packages/robypz-laravel-mongo-roles/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (8)Used By (0)

Laravel MongoDB Roles &amp; Permissions
=======================================

[](#laravel-mongodb-roles--permissions)

Logo
----

[](#logo)

 [![Logo](/art/remove%20the%20checkmark.png)](/art/remove%20the%20checkmark.png)

**Simple roles and permissions for Laravel using MongoDB.**

---

Features
--------

[](#features)

- Middleware for roles and permissions.
- Blade directives for roles and permissions.
- Integration with [mongodb/laravel-mongodb](https://github.com/mongodb/laravel-mongodb).
- Ready-to-use migrations and seeders.
- Easy to extend and customize.

---

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

[](#installation)

1. **Install the package via Composer:**

    ```
    composer require robypz/laravel-mongo-roles
    ```
2. **Publish and run the migrations:**

    ```
    php artisan vendor:publish --provider="RobYpz\MongoRole\Providers\MongoRoleServiceProvider" --tag=migrations
    php artisan migrate
    ```
3. **Add the ServiceProvider if necessary:**

    In `config/app.php`:

    ```
    'providers' => [
        // ...
        RobYpz\MongoRole\Providers\MongoRoleServiceProvider::class,
    ],
    ```

---

Usage
-----

[](#usage)

### Seeders

[](#seeders)

Example seeder to create users, roles, and permissions:

```
use RobYpz\MongoRole\Models\Role;
use RobYpz\MongoRole\Models\Permission;
use Workbench\Database\Factories\UserFactory;

$user = UserFactory::new()->create([
    'name' => 'Test User',
    'email' => 'test@example.com',
]);

$role = Role::create(['name'=> 'role']);
$permission = Permission::create(['name'=> 'permission']);

$role->permissions()->attach($permission);
$user->roles()->attach($role);
```

### Middleware

[](#middleware)

Protect your routes using the middlewares:

```
Route::get('/hasRole', function () {
    return response(null,200);
})->middleware(['auth','role:role']);

Route::get('/hasPermission', function () {
    return response(null,200);
})->middleware(['auth','permission:permission']);

Route::get('/hasAnyRole', function () {
    return response(null,200);
})->middleware(['auth','any_role:role,anotherRole']);

Route::get('/hasAnyPermission', function () {
    return response(null,200);
})->middleware(['auth','any_permission:permission,anotherPermission']);

Route::get('/PermissionTo', function () {
    return response(null,200);
})->middleware(['auth','permission_to:permission']);

Route::get('/hasAnyPermissionTo', function () {
    return response(null,200);
})->middleware(['auth','any_permission_to:permission,anotherPermission']);
```

### Blade Directives

[](#blade-directives)

```
@role('role')
    Welcome user with role
@endrole

@permission('permission')
    Welcome user with permission
@endpermission

@any_role('role,anotherRole')
    Welcome user with any of the roles
@endany_role

@any_permission('permission,anotherPermission')
    Welcome user with any of the permissions
@endany_permission

@permission_to('permission,anotherPermission')
    Welcome user with any of the permissions
@endpermission_to

@any_permission_to('permission,anotherPermission')
    Welcome user with any of the permissions
@end_any_permission_to

```

Traits: HasMongoRoles &amp; HasMongoPermissions
-----------------------------------------------

[](#traits-hasmongoroles--hasmongopermissions)

### HasMongoRoles

[](#hasmongoroles)

Add the `HasMongoRoles` trait to your User model to enable role and permission management via MongoDB.

#### Methods

[](#methods)

- **roles()**
    Returns the roles related to the user.
- **hasRole(string|array $roles): bool**
    Returns `true` if the user has all the specified roles.
- **hasAnyRole(string|array $roles): bool**
    Returns `true` if the user has at least one of the specified roles.
- **hasPermission(string|array $permissions): bool**
    Returns `true` if the user has all the specified permissions through their roles.
- **hasAnyPermission(string|array $permissions): bool**
    Returns `true` if the user has at least one of the specified permissions through their roles.

### HasMongoPermissions

[](#hasmongopermissions)

> This trait provides an alternative approach: assign permissions directly to users, without roles.
> **Note:** `HasMongoPermissions` is already included in `HasMongoRoles`, so you do not need to add both.

#### Methods

[](#methods-1)

- **permissions()**
    Returns the permissions related to the user.
- **hasPermissionTo(string|array $permissions): bool**
    Returns `true` if the user has all the specified direct permissions.
- **hasAnyPermissionTo(string|array $permissions): bool**
    Returns `true` if the user has at least one of the specified direct permissions.

---

Add the trait to your User model:

```
use RobYpz\MongoRole\Traits\HasMongoRoles;

class User extends Authenticatable
{
    use HasMongoRoles; // Includes HasMongoPermissions
    // ...
```

---

Testing
-------

[](#testing)

Includes tests with [Pest](https://pestphp.com/) and [Orchestra Testbench](https://github.com/orchestral/testbench):

```
composer test
```

---

Configuration
-------------

[](#configuration)

You can customize the configuration in [`src/config/mongorole.php`](src/config/mongorole.php).

---

License
-------

[](#license)

MIT © [Robert Yepez](mailto:robertyepez0208@hotmail.com)

---

Logo
----

[](#logo-1)

 [![Logo](/art/remove%20the%20checkmark.png)](/art/remove%20the%20checkmark.png)

---

Package Structure
-----------------

[](#package-structure)

- `src/` Main source code.
- `workbench/` Development and testing environment.
- `tests/` Unit and functional tests.
- `art/` Graphic resources (logo).

---

Questions or suggestions? Open an issue or contact me directly.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance60

Regular maintenance activity

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

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

Every ~6 days

Total

7

Last Release

250d ago

Major Versions

v1.x-dev → v2.x-dev2025-09-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/175afcbc791c015f8e56d10a8f349ffb04b64c8a0b83411a235313a75df6f422?d=identicon)[robypz](/maintainers/robypz)

---

Top Contributors

[![robypz](https://avatars.githubusercontent.com/u/51725616?v=4)](https://github.com/robypz "robypz (43 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/robypz-laravel-mongo-roles/health.svg)

```
[![Health](https://phpackages.com/badges/robypz-laravel-mongo-roles/health.svg)](https://phpackages.com/packages/robypz-laravel-mongo-roles)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[tymon/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

11.5k49.1M350](/packages/tymon-jwt-auth)[php-open-source-saver/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

8359.8M53](/packages/php-open-source-saver-jwt-auth)[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)[scaler-tech/laravel-saml2

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

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

PHPackages © 2026

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