PHP code example of novatopro / lrp

1. Go to this page and download the library: Download novatopro/lrp 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/ */

    

novatopro / lrp example snippets


use NovatoPro\Lrp\Traits\UserLrp;

class User extends Authenticatable
{
    use UserLrp;
}


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

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
bash
php artisan vendor:publish --tag="lrp-migrations"
php artisan migrate