F
Filament14mo ago
Quin.

based on roles only view index and create

Hello! i only want to display the create for the user role . The admin will have acces to index,edit and create Does anyone know how to do this?
5 Replies
einnlleinhatt_
einnlleinhatt_14mo ago
Use policy's
Quin.
Quin.OP14mo ago
You can just add it in the resource just as i wanted to
public static function canCreate(): bool
{
return auth()->user()->hasRole(['admin', 'user']);
}

public static function canEdit(Model $record): bool
{
return auth()->user()->hasRole(['admin']);
}
public static function canCreate(): bool
{
return auth()->user()->hasRole(['admin', 'user']);
}

public static function canEdit(Model $record): bool
{
return auth()->user()->hasRole(['admin']);
}
but i don't know how to do the index page I thought of another way just create another page where we can create the resource. So just did the canViewAny on admin in the resource
vitali
vitali14mo ago
Can you share some code? I didn’t understand how to integrate policies, for example i need only to have 3 user roles and each ones can view or crud some resources
Quin.
Quin.OP14mo ago
public static function canView(): bool { return auth()->user()->hasRole('admin'); } for example you can also do canEdit and canCreate
DrByte
DrByte14mo ago
It's super-simple if you do it via a Laravel model Policy. Similar logic as Quin wrote, but defined in the Policy. That way the access control applies anywhere you use the model, because Filament Resources automatically use the Policy.

Did you find this page helpful?