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
Cannot redeclare App\Models\User::canAccessFilament()The error says, that you already declared that method name. Might be under a different signature.
Got it and How can i get user data inside that function, i want to find his role.
The method is on the user model. So user is
$this
. That part is already right.After it return false instead of 403 error , can i redirect back to login page with smoe error notification.
Well, did you try just adding a redirect if the role isn't right? I guess it should be possible. Notification maybe too
i didn't tried it ate, i don't where to give the redirect and notification , inside this canAccessFilament() function or Authenticate.
Try inside
canAccessFilament()
i can't able to return redirect or notification, it only taking boolen.
You could throw a HttpException that takes a redirect response.
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.