PHP code example of baklysystems / laravel-permission
1. Go to this page and download the library: Download baklysystems/laravel-permission 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/ */
baklysystems / laravel-permission example snippets
//adding permissions to a user
$user->givePermissionTo('edit articles');
//adding permissions via a role
$user->assignRole('writer');
$user2->assignRole('writer');
$role->givePermissionTo('edit articles');
return [
/*
|--------------------------------------------------------------------------
| Authorization Models
|--------------------------------------------------------------------------
*/
'models' => [
/*
|--------------------------------------------------------------------------
| Permission Model
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| Eloquent model should be used to retrieve your permissions. Of course, it
| is often just the "Permission" model but you may use whatever you like.
|
| The model you want to use as a Permission model needs to implement the
| `Spatie\Permission\Contracts\Permission` contract.
|
*/
'permission' => Spatie\Permission\Models\Permission::class,
/*
|--------------------------------------------------------------------------
| Role Model
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| Eloquent model should be used to retrieve your roles. Of course, it
| is often just the "Role" model but you may use whatever you like.
|
| The model you want to use as a Role model needs to implement the
| `Spatie\Permission\Contracts\Role` contract.
|
*/
'role' => Spatie\Permission\Models\Role::class,
],
/*
|--------------------------------------------------------------------------
| Authorization Tables
|--------------------------------------------------------------------------
*/
'table_names' => [
/*
|--------------------------------------------------------------------------
| Roles Table
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your roles. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'roles' => 'roles',
/*
|--------------------------------------------------------------------------
| Permissions Table
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your permissions. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'permissions' => 'permissions',
/*
|--------------------------------------------------------------------------
| User Permissions Table
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your users permissions. We have chosen a
| basic default value but you may easily change it to any table you like.
|
*/
'user_has_permissions' => 'user_has_permissions',
/*
|--------------------------------------------------------------------------
| User Roles Table
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your users roles. We have chosen a
| basic default value but you may easily change it to any table you like.
|
*/
'user_has_roles' => 'user_has_roles',
/*
|--------------------------------------------------------------------------
| Role Permissions Table
|--------------------------------------------------------------------------
|
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your roles permissions. We have chosen a
| basic default value but you may easily change it to any table you like.
|
*/
'role_has_permissions' => 'role_has_permissions',
],
];
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);