1. Go to this page and download the library: Download coyote6/laravel-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/ */
coyote6 / laravel-permissions example snippets
return [
'tables' => [
'permissions' => 'permissions',
'roles' => 'roles',
'role-permissions' => 'role_permissions',
'user-roles' => 'user_roles',
'users' => 'users' // This needs to match your user table name
]
];
use Coyote6\LaravelPermissions\Traits\HasRoles;
use Coyote6\LaravelPermissions\Traits\UserRoles;
class User extends Authenticatable {
// Add the traits.
use HasRoles,
UserRoles;
}
use Coyote6\LaravelPermissions\Traits\AdminAccessTrait;
class ExamplePolicy {
use AdminAccessTrait;
}
$u = User::find(1);
$u->addRole('administrator');
use Coyote6\LaravelPermissions\Models\Permissions;
use Coyote6\LaravelPermissions\Models\Models;
$permission = Permission::create (['name' => 'Name of Permission']); // A machine name id will automatically be created.
$role = Role::create (['name' => 'Name of Permission']); // A machine name id will automatically be created.
$role->addPermission($permission);
class ExamplePolicy {
use AdminAccessTrait,
HandlesAuthorization;
public function view (User $user) {
//
// Example using the machine name id for the permission
//
if ($user->hasPermission ('do_something')) {
return true;
}
return false;
}
public function create (User $user) {
//
// Example using the name for the permission
//
if ($user->hasPermission ('Create Example', 'name')) {
return true;
}
return false;
}
}
<div>
@usercan ('do_something')
<p>Show if user can do something</p>
@endusercan
</div>
<div>
@usercan ('do_something|do_something_else')
<p>Show if user can do something or do something else</p>
@endusercan
</div>
<div>
@useris (1)
<p>Show if user is user - 1</p>
@enduseris
</div>
<div>
@useris ('1|2|3')
<p>Show if user is user - 1, 2, or 3</p>
@enduseris
</div>
<div>
@userisandcan (1, 'do_something')
<p>Show if user is user - 1</p>
@enduserisandcan
</div>
<div>
@userisandcan ('1|2|3', 'do_something|do_something_else')
<p>Show if user is user - 1,2, or 3 and can do something or can do something else</p>
@enduserisandcan
</div>
<div>
@authororcan (1, 'do_something', 'do_something_else')
<p>Show if user is user - 1 and can do something or the user is any user who can do something else</p>
@endauthororcan
</div>
<div>
@authororcan ('1|2|3', 'do_something|do_something_else', 'do_some_other_thing_1|do_some_other_thing_2')
<p>Show if user is user - 1,2, or 3 and can do something or can do something else or if the user is any user who can do some other thing 1 or 2.</p>
@endauthororcan
</div>
namespace App\Policies;
use App\Traits\StandardPolicy;
use Illuminate\Auth\Access\HandlesAuthorization;
class ModelPolicy {
use StandardPolicy,
HandlesAuthorization;
protected string $modelPermissionName = 'models';
}