Filament Access

What I am trying to do: I want give filament access to the particular user role. What I did: I used public function canAccessFilament(): bool inside the user model. My issue/the error: Cannot redeclare App\Models\User::canAccessFilament() Code: public function canAccessFilament(): bool { return $this->role != 'member'; }
10 Replies
Dennis Koch
Dennis Koch13mo ago
Cannot redeclare App\Models\User::canAccessFilament()
The error says, that you already declared that method name. Might be under a different signature.
Hemanath
HemanathOP13mo ago
Got it and How can i get user data inside that function, i want to find his role.
Dennis Koch
Dennis Koch13mo ago
The method is on the user model. So user is $this. That part is already right.
Hemanath
HemanathOP13mo ago
After it return false instead of 403 error , can i redirect back to login page with smoe error notification.
Dennis Koch
Dennis Koch13mo ago
Well, did you try just adding a redirect if the role isn't right? I guess it should be possible. Notification maybe too
Hemanath
HemanathOP13mo ago
i didn't tried it ate, i don't where to give the redirect and notification , inside this canAccessFilament() function or Authenticate.
Dennis Koch
Dennis Koch13mo ago
Try inside canAccessFilament()
Hemanath
HemanathOP13mo ago
i can't able to return redirect or notification, it only taking boolen.
Dennis Koch
Dennis Koch13mo ago
You could throw a HttpException that takes a redirect response.
Hemanath
HemanathOP13mo ago
I have done this: public function canAccessFilament(): bool { $user = User::find($this->id); if ($user->hasRole('member')) { return false; } elseif ($user->hasRole('owner')) { $franchisee = $user->franchisees->first(); if ($franchisee->block_franchisee == 'Y' || $franchisee->terminate_franchisee == 'Y') { if(Route::currentRouteName() == 'filament.pages.dashboard'){ throw new HttpResponseException(redirect()->route('filament.auth.login')->with('error', 'Custom error message')); }else{ return true; } // throw new HttpException(403, 'You do not have access to test.'); } else { return true; } } else { return true; } } to redirect it to the login page , but it is redirecting in aloop. i tried to set if condition to not run in a loop,but it not worked.
Want results from more Discord servers?
Add your server