Tricks

Scoping policies for Filament using isServing()

May 12, 2023
Alex Justesen
Admin panel

In v2.17.25 a new method was introduced called Filament::isServing(). This makes it really easy to scope policies for Filament Resources separate from the logic used in controllers or the frontend.

Example: Here we want to allow a user to be updated in Filament based on a permission but in the frontend only the authenticated user can update themselves.

public function update(User $user, User $model): bool
{
if (Filament::isServing()) {
return $user->can('update user');
}
 
/**
* A user can update themselves.
*/
return $user->id === $model->id;
}

No comments yet…