PHPackages                             mark-villudo/laravel-user-management - 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. mark-villudo/laravel-user-management

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

mark-villudo/laravel-user-management
====================================

Permission handling for Laravel 5.1 and up

1(7y ago)22.0k2[1 issues](https://github.com/MarkVilludo/laravel-user-management/issues)MITCSSPHP &gt;=5.6.0

Since Nov 5Pushed 7y ago1 watchersCompare

[ Source](https://github.com/MarkVilludo/laravel-user-management)[ Packagist](https://packagist.org/packages/mark-villudo/laravel-user-management)[ Docs](https://github.com/MarkVilludo/laravel-user-management)[ RSS](/packages/mark-villudo-laravel-user-management/feed)WikiDiscussions updated\_layout Synced 2d ago

READMEChangelog (1)Dependencies (5)Versions (4)Used By (0)

Associate users with roles and permissions
==========================================

[](#associate-users-with-roles-and-permissions)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8dac7f853825c1340b3a9d9a7905a75e01876fb19df42e145bf076a27d61f444/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-permission)[![Build Status](https://camo.githubusercontent.com/6e5e5edc60dd0091987a71b67959130e969d9d6dfb3ed7c3d4ad220c9239e91e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d7065726d697373696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-permission)[![Quality Score](https://camo.githubusercontent.com/64b055fbd73608ab336dc0a10b639327b4b0ca2d0a7009eb354ed0890d3c8e5e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-permission)[![StyleCI](https://camo.githubusercontent.com/cc51255e9a836dd8165e58f1946d0f5b0db78f3f7a5a0f3e22ae65e170109892/68747470733a2f2f7374796c6563692e696f2f7265706f732f34323438303237352f736869656c64)](https://styleci.io/repos/42480275)[![Total Downloads](https://camo.githubusercontent.com/ad5f5474cf3f24431e97d50a8db551720b656aaf774137db2ebcb4ac074fa16e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-permission)

This package allows to save permissions and roles in a database. It is built upon [Laravel's authorization functionality](http://laravel.com/docs/5.1/authorization) that was [introduced in version 5.1.11](http://christoph-rumpel.com/2015/09/new-acl-features-in-laravel/).

Once installed you can do stuff like this:

```
// Assign role to specific user
$user->assignRole('writer');
```

You can test if a user has a permission with specific permission and module name.

```
$user->canAccess('View','Users');
```

Licensed
--------

[](#licensed)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a message.

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

[](#installation)

You can install the package via Composer:

```
composer require mark-villudo/laravel-user-management
```

Now add the service provider in `config/app.php` file:

```
'providers' => [
    // ...
    MarkVilludo\Permission\PermissionServiceProvider::class,
];
```

You can publish the migration with:

```
php artisan vendor:publish --provider="MarkVilludo\Permission\PermissionServiceProvider" --tag="migrations"
```

The package assumes that your users table name is called "users". If this is not the case you should manually edit the published migration to use your custom table name.

After the migration has been published you can create the role- and permission-tables by running the migrations:

```
$ php artisan migrate
```

You can publish Initial Permission Seeder, then update it's content depending on your needs.

```
php artisan vendor:publish --provider="MarkVilludo\Permission\PermissionServiceProvider" --tag="seeder"
```

You can publish the config-file with:

```
php artisan vendor:publish --provider="MarkVilludo\Permission\PermissionServiceProvider" --tag="config"
```

This is the contents of the published `config/laravel-permission.php` config file:

```
return [

    'models' => [

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your permissions. Of course, it
         * is often just the "Permission" model but you may use whatever you like.
         *
         * The model you want to use as a Permission model needs to implement the
         * `MarkVilludo\Permission\Contracts\Permission` contract.
         */

        'permission' => MarkVilludo\Permission\Models\Permission::class,

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your roles. Of course, it
         * is often just the "Role" model but you may use whatever you like.
         *
         * The model you want to use as a Role model needs to implement the
         * `MarkVilludo\Permission\Contracts\Role` contract.
         */

        'role' => MarkVilludo\Permission\Models\Role::class,

    ],

    'table_names' => [

        /*
         * The table that your application uses for users. This table's model will
         * be using the "HasRoles" and "HasPermissions" traits.
         */

        'users' => 'users',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your roles. We have chosen a basic
         * default value but you may easily change it to any table you like.
         */

        'roles' => 'roles',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your permissions. We have chosen a basic
         * default value but you may easily change it to any table you like.
         */

        'permissions' => 'permissions',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your users permissions. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'user_has_permissions' => 'user_has_permissions',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your users roles. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'user_has_roles' => 'user_has_roles',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your roles permissions. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'role_has_permissions' => 'role_has_permissions',
    ],

    'foreign_keys' => [

        /*
         * The name of the foreign key to the users table.
         */
        'users' => 'user_id',
    ],

    /*
     *
     * By default we'll make an entry in the application log when the permissions
     * could not be loaded. Normally this only occurs while installing the packages.
     *
     * If for some reason you want to disable that logging, set this value to false.
     */

    'log_registration_exception' => true,
];
```

You can publish the views with:

```
php artisan vendor:publish --provider="MarkVilludo\Permission\PermissionServiceProvider" --tag="views"
```

You can publish the public assets with:

```
php artisan vendor:publish --provider="MarkVilludo\Permission\PermissionServiceProvider" --tag="assets"
```

Usage
-----

[](#usage)

First add the `MarkVilludo\Permission\Traits\HasRoles` trait to your User model, then paste the ff code below.

```
