PHP code example of sefirosweb / laravel-access-list
1. Go to this page and download the library: Download sefirosweb/laravel-access-list 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/ */
// app/Models/User.php
namespace App\Models;
use Sefirosweb\LaravelAccessList\Http\Models\User as AccessListUser;
class User extends AccessListUser
{
// Your own casts, fillable, scopes, accessors...
}
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function roles(): BelongsToMany
{
return $this->belongsToMany(
config('laravel-access-list.Role'),
'user_has_role'
);
}
// Optional helpers if you want to call $user->hasAcl('foo') from your code:
public function hasAcl(string $acl): bool { /* ... */ }
}
use Sefirosweb\LaravelAccessList\Http\Models\AccessList;
use Sefirosweb\LaravelAccessList\Http\Models\Role;
$acl = AccessList::create(['name' => 'reports_view', 'description' => 'View reports']);
$role = Role::create(['name' => 'analyst', 'description' => 'Analytics staff']);
$role->access_lists()->attach($acl);
$user->roles()->attach($role);
// Now the analyst can access any route protected by `checkAcl:reports_view`.