PHP code example of bramalho / laravel-user-permissions

1. Go to this page and download the library: Download bramalho/laravel-user-permissions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bramalho / laravel-user-permissions example snippets


BRamalho\LaravelUserPermissions\LaravelUserPermissionsServiceProvider::class,



namespace App;

use BRamalho\LaravelUserPermissions\Traits\Permission;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, Permission;

    //...
}



namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use BRamalho\LaravelUserPermissions\Http\Middleware\UserHasRole;
use BRamalho\LaravelUserPermissions\Http\Middleware\UserHasPermission;

class Kernel extends HttpKernel
{
    // ...

    protected $routeMiddleware = [
        // ...
        'role' => UserHasRole::class,
        'permission' => UserHasPermission::class
    ];
}



use Illuminate\Database\Seeder;
use App\User;
use BRamalho\LaravelUserPermissions\Models\UserRole;
use BRamalho\LaravelUserPermissions\Models\UserPermission;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        User::create([
            'id' => 1,
            'name' => 'Admin',
            'email' => '[email protected]',
            'password' => bcrypt('admin')
        ]);
        User::create([
            'id' => 2,
            'name' => 'Publisher',
            'email' => '[email protected]',
            'password' => bcrypt('publisher')
        ]);

        UserRole::create(
            [
                'id'    => 1,
                'name'  => 'Admin'
            ]
        );
        UserRole::create(
            [
                'id'    => 2,
                'name'  => 'Publisher'
            ]
        );

        UserPermission::create(
            [
                'id'            => 1,
                'user_role_id'  => 2,
                'module'        => 'pages',
                'action'        => 'view'
            ]
        );
        UserPermission::create(
            [
                'id'            => 2,
                'user_role_id'  => 2,
                'module'        => 'pages',
                'action'        => 'add'
            ]
        );
        UserPermission::create(
            [
                'id'            => 3,
                'user_role_id'  => 2,
                'module'        => 'pages',
                'action'        => 'edit'
            ]
        );

        $userAdmin = User::find(1);
        $roleAdmin = UserRole::find(1);
        $userAdmin->assignRole($roleAdmin);
        //$userPublisher->removeRole($roleAdmin);

        $userPublisher = User::find(2);
        $rolePublisher = UserRole::find(2);
        $userPublisher->assignRole($rolePublisher);
        //$userPublisher->removeRole($rolePublisher);
    }
}

Route::get('/page/{id}', 'PageController@index')->name('page')->middleware('permission:pages,view');
Route::get('/page/{id}/add', 'PageController@add')->name('pageAdd')->middleware('permission:pages,add');
Route::get('/page/{id}/edit', 'PageController@add')->name('pageAdd')->middleware('permission:pages,edit');
Route::delete('/page/{id}/delete', 'PageController@delete')->name('pageDelete')->middleware('permission:pages,delete');
sh
php artisan vendor:publish --provider 'BRamalho\LaravelUserPermissions\LaravelUserPermissionsServiceProvider'
sh
php artisan migrate