sethbr11
sethbr11
FFilament
Created by sethbr11 on 1/22/2025 in #❓┊help
View and ViewAny Permissions
I have a custom menu item on my backend which has this logic for visibility:
->visible(function () {
$location = Location::find(auth()->user()->location_id);
$canView = auth()->user()->can('view', $location);
$canViewAll = auth()->user()->can('viewAny', Location::class);
return $canView && !$canViewAll;
})
->visible(function () {
$location = Location::find(auth()->user()->location_id);
$canView = auth()->user()->can('view', $location);
$canViewAll = auth()->user()->can('viewAny', Location::class);
return $canView && !$canViewAll;
})
I did this so it doesn't show for super admins, who are able to see the actual page with all of the locations. This seems to work, but when I'm in as a role who has view permissions but not viewAny and I click on the menu item, I get a 403 page. I can't seem to figure out why I can't view the location page. This is my location policy:
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->can('view_any_location');
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Location $location): bool
{
return $user->can('view_location');
}
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->can('view_any_location');
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Location $location): bool
{
return $user->can('view_location');
}
Eventually I'd want to change this so it restricts it to only being able to view their location, but right now it's just not letting me access the page or any of the locations. Any help here?
1 replies
FFilament
Created by sethbr11 on 1/18/2025 in #❓┊help
Any way to change the way the options for the TrashedFilter display?
Instead of them saying "deleted", I want to replace the words with "disabled". I've been able to replace the main label for the filter pretty easily, but I'm not sure what the values for the options are already, and any attempt to just reassign the values of ->options have been unsuccessful so far. I'd prefer to not have to remake it all if possible
4 replies
FFilament
Created by sethbr11 on 1/6/2025 in #❓┊help
Filter Not Returning Anything When Nothing is Selected
I am trying to filter my contacts based on what subscription type they have, which is stored in a different table but has foreign key relationships. Here is my code so far:
->modifyQueryUsing(fn (Builder $query) => $query->where('location_id', '=', Session::get('location_id')))
->filters([
SelectFilter::make('plan_id')
->label('Plan')
->placeholder('Filter by Plan')
->multiple()
->options(function () {
$locationId = Session::get('location_id');
return Plan::whereHas('subscriptionsPivot', function ($query) use ($locationId) {
$query->where('location_id', $locationId);
})->pluck('name', 'id')->toArray();
})
->query(function (Builder $query, array $data) : Builder {
// Check if plan_id is explicitly set in the filter
if ($data && isset($data['plan_id'])) {
return $query->whereHas('subscriptionsPivot', function (Builder $query) use ($data) {
$query->whereIn('plan_id', Arr::flatten($data));
});
}
return $query;
}),
])
->modifyQueryUsing(fn (Builder $query) => $query->where('location_id', '=', Session::get('location_id')))
->filters([
SelectFilter::make('plan_id')
->label('Plan')
->placeholder('Filter by Plan')
->multiple()
->options(function () {
$locationId = Session::get('location_id');
return Plan::whereHas('subscriptionsPivot', function ($query) use ($locationId) {
$query->where('location_id', $locationId);
})->pluck('name', 'id')->toArray();
})
->query(function (Builder $query, array $data) : Builder {
// Check if plan_id is explicitly set in the filter
if ($data && isset($data['plan_id'])) {
return $query->whereHas('subscriptionsPivot', function (Builder $query) use ($data) {
$query->whereIn('plan_id', Arr::flatten($data));
});
}
return $query;
}),
])
The main problem has been the if ($data && isset($data['plan_id'])) line. If I just have if($data), it seems to always evaluate to true, even if I don't have a filter option selected, which means the filters themselves work but when nothing is selected nothing shows up. If I try the line as it is in my code, everyone shows up when nothing is selected—even those without a plan, which is what I want—but isset($data['plan_id']) seems to always evaluate to false, so then the filters don't do anything. I'm pretty new to Filament. Any tips?
7 replies