Filament: Hide Relation Manager by Condition

In Filament, sometimes you need to show/hide elements based on a condition, like user's role. In this tutorial, we will see how to hide/disable Filament Relation Manager if the record doesn't belong to the logged-in user.

This apartment does not belong to us, so how can we hide the Filament Relation Manager?


Project

In our project, we have two connected Resources:

  • Apartments - Where people put their apartment information (in our case, this is listing information)
  • Apartment Documents - A relation manager where we should be able to upload private documents without showing the relation manager to unauthorized users.

This is how it looks right now:

app/Filament/Resources/ApartmentResource.php

// ...
 
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('user_id')
->relationship('user', 'name')
->required(),
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\TextInput::make('price')
->required()
->numeric()
->prefix('$'),
Forms\Components\TextInput::make('bedrooms')
->required()
->numeric(),
]);
}
 
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('user.name')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('price')
->money()
->sortable(),
Tables\Columns\TextColumn::make('bedrooms')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
 
public static function getRelations(): array
{
return [
RelationManagers\ApartmentDocumentsRelationManager::class
];
}
 
// ...

And our ApartmentDocumentsRelationManager looks like this:

app/Filament/Resources/ApartmentResource/RelationManagers/ApartmentDocumentsRelationManager.php

// ...
 
protected static string $relationship = 'apartmentDocuments';
 
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\FileUpload::make('file')
->required(),
Forms\Components\TextInput::make('description')
->required()
->maxLength(255),
]);
}
 
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('description')
->columns([
Tables\Columns\TextColumn::make('file'),
Tables\Columns\TextColumn::make('description'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
 
// ...

While this is a simple example, we still want to hide the Relation Manager! So let's do that next.


Hiding the Relation Manager

To hide the Relation manager - we can use the function canViewForRecord():

app/Filament/Resources/ApartmentResource/RelationManagers/ApartmentDocumentsRelationManager.php

use Illuminate\Database\Eloquent\Model;
 
// ...
 
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
return $ownerRecord->user_id === auth()->id();
}

If we reload the page, we will see that the Relation Manager is hidden!

If we open the apartment assigned to our user, we will see the Relation Manager:

That's it! You have successfully hidden the Relation Manager based on a condition. While this was a simple example, here are the key points:

  • The canViewForRecord() method controls whether the User can see the Relation Manager.
  • $ownerRecord will give us the owner Record (in our case, this was the Apartment).
  • You can add any condition to the canViewForRecord() method if it returns a boolean.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 60 courses (1084 lessons, total 42 h 44 min)
  • 80 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials