canAccess function generate a lot of duplicate queries

Hello, I have a custom page in filament and I want to restrict acces to it only for some users, for this I'm using the canAccess function from the filament Page class this is the condition: public static function canAccess(): bool { $event_user = Filament::getTenant()->eventContestants()->where('user_id', auth()->id())->get(); return !$event_user->isEmpty(); } and for some reason this generates a lot of duplicate queries, the example from the screenshot is with a few dummy data, but on live env there are like 180 duplicate queries and the page is loading super slow (± 5sec) I'm doing something wrong? Maybe I don't use filament as it should be used? Any advice? Thank you
No description
3 Replies
ChesterS
ChesterS3w ago
Is this in a table or a list of sorts? You might need to refactor how you check what people can see in that page - instead of checking if they have access to each event one-by-one, maybe only show the events they do have access to and skip the canAccess() check entirely? Or maybe eager-load the event contestants and use a different policy?
Depends o what you're trying to achieve. Also, your query can be improved.
public static function canAccess(): bool
{
return Filament::getTenant()->eventContestants()->where('user_id', auth()->id())->exists();
}
public static function canAccess(): bool
{
return Filament::getTenant()->eventContestants()->where('user_id', auth()->id())->exists();
}
Some other improvements can probably be made, but I've never used tenancies.
Tudor
TudorOP3w ago
its a custom livewire page that extends the filament page component, and I thought that canAccess is executed only once the page is loaded, but for some reason it duplicate that query for each user, I dont know exactly A solution was to move the verification to the mount function and abort 403 manually
ChesterS
ChesterS3w ago
It didn't duplicate for every user, it did for every event. In any case, it doesn't matter since you found a workaround 👍
Want results from more Discord servers?
Add your server