F
Filament3mo ago
morty

Struggling with an "active" filter and scope on a resource

I have an account resource. The database table has a inactive_flag column that is a Y or N value. I want my account resource table to be only active accounts by default, and the ability to include inactive with a filter. I also want my global search to only search active accounts, and the view page of the account to not care whether it's active or inactive. At first I implemented a global scope to scope active only by default. This works great for the table listing and global search. I can use a filter to include inactive by removing the global scope on the base query. However, my problem occurs when I want to navigate to the view page of the account, then the global scope applies again and I get a 404. I'm not sure how I can remove the global scope on the view page of the account? Alternatively I tried without global scopes and modifying the initial query of the table to include ->where('inactive_flag', '!=', 'Y') which works, but then my filter doesn't work to include inactive because if I use either the query or baseQuery methods, it still results in a query that does this: where inactive_flag != 'Y' and inactive_flag = 'Y' which obviously no accounts will match. How do I solve this?
1 Reply
morty
morty3mo ago
I'm not a fan of the global scope so I've backtracked on that plan. I currently have no global scopes on the model but I do have a local scope and I'm modifying the global search with the following:
public static function getGlobalSearchEloquentQuery(): Builder
{
return parent::getGlobalSearchEloquentQuery()->active();
}
public static function getGlobalSearchEloquentQuery(): Builder
{
return parent::getGlobalSearchEloquentQuery()->active();
}
This has global search working properly, and view pages working properly. However, the table and filter I can't figure out. I'm semi-solved my issues. I ended up using a default filter of active instead of modifying the table query. I switched to a ternary filter instead of a toggle. Not exactly what I had in mind but it's working.