user()->can('viewAny', User::class)) { $this->users = User::all(); $this->invitations = Invitation::all(); } } public function inviteUser() { $this->authorize('invite', User::class); $inv = Invitation::create([ 'code' => str()->random(50), 'email' => $this->invite_email, 'invited_by' => auth()->user()->id, 'expires_at' => now()->addDays(7), ]); Flux::modal('invite-user')->close(); // Refresh the data $this->invitations = Invitation::all(); $this->invite_email = ''; } public function deleteUser(User $user) { $this->authorize('delete', $user); $user->delete(); // Refresh the data $this->users = User::all(); } public function changeUserRole(User $user, string $role) { $this->authorize('update', $user); // Prevent admins from demoting themselves if ($user->id === auth()->id() && $role === 'user') { session()->flash('error', 'You cannot demote yourself from admin.'); return; } $isAdmin = $role === 'admin'; $user->update(['is_admin' => $isAdmin]); // Refresh the data $this->users = User::all(); session()->flash('success', "User role updated to {$role}."); } public function render() { return view('livewire.manage-users'); } }