LaravelPackages.net
Acme Inc.
Toggle sidebar
novatopro/lrp

This is my package lrp

11
0
0.0.3
About novatopro/lrp

novatopro/lrp is a Laravel package for this is my package lrp. It currently has 0 GitHub stars and 11 downloads on Packagist (latest version 0.0.3). Install it with composer require novatopro/lrp. Discover more Laravel packages by novatopro or browse all Laravel packages to compare alternatives.

Last updated

This is a basic package for laravel roles and permissions

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provide basic roles and permissions, this readme.md contains examples of usage.

Installation

You can install the package via composer:

composer require novatopro/lrp

You can publish and run the migrations with:

php artisan vendor:publish --tag="lrp-migrations"
php artisan migrate

Usage

Update user model relationship

app\Models\User.php

use NovatoPro\Lrp\Traits\UserLrp;

class User extends Authenticatable
{
    use UserLrp;
}

Update boot method in AuthServiceProvider

app\Providers\AuthServiceProvider.php

Gate::define('access', function (User $user, ...$permissions) {
    return $user->hasPermissions($permissions);
});

Examples

use NovatoPro\Lrp\Models\Role;
use NovatoPro\Lrp\Models\Permission;
// Example user credentials
$credentials = [
    'name'=>'Example User',
    'email'=>'[email protected]',
    'password'=>'password'
];

// Create example user
$user = User::updateOrCreate(['email'=>$credentials['email']],['name'=>$credentials['name'],'password'=>Hash::make($credentials['password'])]);

// Create example role
$role = Role::updateOrCreate(['name'=>'Developer Features','slug'=>'developer-features']);

// Create example permission
$permission = Permission::updateOrCreate(['name'=>'Dev','slug'=>'dev','description'=>'Can see features in development']);

// Add permision to role without remove, without duplicate
$role->permissions()->syncWithoutDetaching($permission->id);

// Add role to user without remove, without duplicate
$user->roles()->syncWithoutDetaching($role->id);

// Check permissions in controllers
if($user->can('access', ['developer','dev','develop'])){
    // Can see features in development
}else{
    // Can't see features in development
}

// Authorize with permissions
use Illuminate\Support\Facades\Gate;
Gate::authorize('access','dev');
// Check permissions in blade
@can('access', ['developer','dev','develop'])
    <h1>Can see features in development</h1>
@else
    <h1>Can't see features in development</h1>
@endcan

License

The MIT License (MIT). Please see License File for more information.

Comments