-
composer.json
Open in GitHub{ "require": { "php": "^8.0", // "spatie/laravel-permission": "^4.2", }, }
-
app/Models/User.php
Open in GitHubuse Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; }
-
app/Policies/UserPolicy.php
Open in GitHubuse App\Models\User; class UserPolicy { use HandlesAuthorization; public function view(User $authenticatedUser, User $user) { return Auth::user()->hasRole('admin') || $authenticatedUser->id === $user->id; } }
-
app/Http/Controllers/Dashboard/User/UserController.php
Open in GitHubuse App\Models\User; use App\Services\UserService; class UserController extends Controller { public function __construct(protected UserService $userSrvc) { $this->middleware('role:admin')->only('view'); } public function edit(User $user) { $this->authorize('view', $user); return view('backend.user.profile', compact('user')); } }