1. Go to this page and download the library: Download httpoz/roles 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/ */
httpoz / roles example snippets
use HttpOz\Roles\Traits\HasRole;
use HttpOz\Roles\Contracts\HasRole as HasRoleContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements HasRoleContract
{
use Notifiable, HasRole;
///
}
$adminRole = \HttpOz\Roles\Models\Role::create([
'name' => 'Admin',
'slug' => 'admin',
'description' => 'Custodians of the system.', // optional
'group' => 'default' // optional, set as 'default' by default
]);
$moderatorRole = \HttpOz\Roles\Models\Role::create([
'name' => 'Forum Moderator',
'slug' => 'forum.moderator',
]);
use App\User;
$user = User::find($id);
$user->attachRole($adminRole); // you can pass whole object, or just an id
$user->detachRole($adminRole); // in case you want to detach role
$user->detachAllRoles(); // in case you want to detach all roles
$user = App\User::find($id);
$roles = [1, 4, 6]; // using the role IDs we want to assign to a user
$user->syncRoles($roles); // you can pass Eloquent collection, or just an array of ids
if ($user->isRole('admin')) { // you can pass an id or slug
// do something
}
// or
if($user->hasRole('admin')) {
// do something
}
// or
if ($user->isAdmin()) {
//
}
if ($user->isRole('admin|forum.moderator')) {
// do something
}
if($user->isRole('admin, forum.moderator')){
// do something
}
if($user->isRole(['admin', 'forum.moderator'])){
// do something
}
if($user->isOne('admin|forum.moderator')){
// do something
}
if($user->isOne('admin, forum.moderator')){
// do something
}
if($user->isOne(['admin', 'forum.moderator'])){
// do something
}
if ($user->isRole('admin|forum.moderator', true)) {
// do something
}
if($user->isRole('admin, forum.moderator', true)){
// do something
}
if($user->isRole(['admin', 'forum.moderator'], true)){
// do something
}
if($user->isAll('admin|forum.moderator')){
// do something
}
if($user->isAll('admin, forum.moderator')){
// do something
}
if($user->isAll(['admin', 'forum.moderator'])){
// do something
}
if ($user->group() == 'application.managers') {
//
}
if ($user->inGroup('application.managers')) {
// if true do something
}
@role('admin') // @if(Auth::check() && Auth::user()->isRole('admin'))
// user is admin
@endrole
@group('application.managers') // @if(Auth::check() && Auth::user()->group() == 'application.managers')
// user belongs to 'application.managers' group
@endgroup
@role('admin|moderator', 'all') // @if(Auth::check() && Auth::user()->isRole('admin|moderator', 'all'))
// user is admin and also moderator
@else
// something else
@endrole