PHP code example of pierresilva / laravel-sentinel

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

    

pierresilva / laravel-sentinel example snippets


pierresilva\Sentinel\SentinelServiceProvider::class,

'Sentinel' => pierresilva\Sentinel\Facades\Sentinel::class,

 'roles' => \pierresilva\Sentinel\Middleware\UserHasRole::class,
 'permissions' => \pierresilva\Sentinel\Middleware\UserHasPermission::class,



namespace App;

use pierresilva\Sentinel\Traits\SentinelTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;
    use SentinelTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'gender', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Auth::user()->isRole('administrator');

Auth::user()->isAdministrator();
 
Auth::user()->can('access.admin');

Auth::user()->can(['access.admin', 'view.users']);

Auth::user()->assignRole(1);
revokeRole($roleId)

Auth::user()->revokeRole(1);

Auth::user()->revokeAllRoles();

Auth::user()->syncRoles([1, 2, 3]);

$role = Role::find(1);
return $role->can('access.admin');
 
$role = Role::find(1);
return $role->getPermissions();

$role = Role::find(1);
$role->assignPermission(1);
$role->save();

$role = Role::find(1);
$role->revokePermission(1);
$role->save();

$role = Role::find(1);
$role->revokeAllPermissions();
$role->save();

$role = Role::find(1);
$role->syncPermissions([1, 2, 3]);
$role->save();

if (Sentinel::can('create.blog.post')) {
    // Do whatever
}
if (Sentinel::can(['access.admin', 'create.blog.post'])) {
    // Do whatever
}

if (Sentinel::canAtLeast(['edit.blog.post', 'create.blog.post'])) {
    // Can at least do one of the tasks
}

if (Sentinel::isRole('admin')) {
    // Do whatever
}

@can('create.blog.post')
    Do whatever
@else
    Do something else
@endcan

@canatleast(['edit.blog.post', 'create.blog.post'])
    Do whatever
@else
    Do something else
@endcanatleast

@role('admin')
    Do whatever
@else
    Do something else
@endrole

php artisan vendor:publish

php artisan migrate