Skip to main content

Restrict Add/Edit/Delete Actions and Buttons

Premium
4:26

Text Version of the Lesson

In this lesson, let's look at simple ways to hide action buttons from the Filament table.


Restrict Create/Delete on Users

Let's imagine we have a User Management section, but we can't create a new user (they register themselves) or delete the existing one (for history archive reasons). The only thing we can do is edit the user's data.

So, I've generated a resource:

php artisan make:filament-resource User

And added a few columns to the form/table:

app/Filament/Resources/UserResource.php:

class UserResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\TextInput::make('email')
->email(),
]);
}
 
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('created_at')
->dateTime(),
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
 
public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}
}

Visually it looks like this:

Now, let's remove the ability to Add and Delete users.


Restrict Adding New Records

To hide the Create form and all links to it, we need to make four changes:

Step 1. Resource method getPages(): delete the create page.

return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];

Step 2. In the Resource Table, remove the...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

D
Daniel ✓ Link copied!

a nice addition is how to add softdeletes to the action. for archive reasons you probably dont want to delete the complete entity but softdeletes/archive however would be a great addition

P
paulknisely ✓ Link copied!

Wouldn't you just add the SoftDeletes trait to the model and add the softdeletes() method to the migration?

PK
Povilas Korop ✓ Link copied!
I
IRESIS ✓ Link copied!

How to hide a create button depends on user role?

M
Modestas ✓ Link copied!

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.