How can I set a condition for a filter to be used on a resource table
I have a resource which has a filter that limits the displayed entries to those created by the logged in user. Is there a way to put a condition where this filter only takes effect if the logged in user is not an admin. Otherwise it should display all entries
3 Replies
Can you post 1. your filter code, and 2. the logic you would use to determine if the user is an admin.
1) Filter::make('is_owner')
->query(function ( $query) {
return $query
->when(
fn ($query) => $query->where('user_id', auth()->user()->id),
);
})->default()
2)auth()->user()->user_role ==10
I'd probably try expanding your shorthand
fn
to a full function
closure so you can early-return $query (unaltered) if the user_role ==10, else pass $query->where(...) as you're doing now.
Or you could try putting your user_role==10 condition check into your call to default() (er, make it falsy, ie != 10) so that the filter doesn't get applied by default