How add a query to the table

In this table, I need to display only the users where is_super in the database is set to false. I just started using Filament today. Can someone help me?
public static function table(Table $table): Table
{
return $table

->columns([
Tables\Columns\TextColumn::make('name')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('email')
->searchable()
->sortable(),
Tables\Columns\ToggleColumn::make('active')
->label('Ativo')
->sortable(),
Tables\Columns\ToggleColumn::make('is_admin')
->label('Admin')
->sortable()
->disabled(fn (?Model $record) => $record?->id === Auth::id()),
Tables\Columns\TextColumn::make('created_at')
->searchable()
->sortable()
->dateTime()
->formatStateUsing(fn ($state) => \Carbon\Carbon::parse($state)->format('d/m/y H:i')),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
])

,
]);

}
public static function table(Table $table): Table
{
return $table

->columns([
Tables\Columns\TextColumn::make('name')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('email')
->searchable()
->sortable(),
Tables\Columns\ToggleColumn::make('active')
->label('Ativo')
->sortable(),
Tables\Columns\ToggleColumn::make('is_admin')
->label('Admin')
->sortable()
->disabled(fn (?Model $record) => $record?->id === Auth::id()),
Tables\Columns\TextColumn::make('created_at')
->searchable()
->sortable()
->dateTime()
->formatStateUsing(fn ($state) => \Carbon\Carbon::parse($state)->format('d/m/y H:i')),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
])

,
]);

}
19 Replies
Abmael Souza
Abmael Souza2w ago
you would have to rewrite the "query" function on the same file you are on
public static function query(): Builder
{
// Filter users where is_super is false
return parent::query()->where('is_super', false);
}
public static function query(): Builder
{
// Filter users where is_super is false
return parent::query()->where('is_super', false);
}
guiiz
guiizOP2w ago
it didnt worked
guiiz
guiizOP2w ago
No description
guiiz
guiizOP2w ago
No description
Abmael Souza
Abmael Souza2w ago
how is your model setup? you didin't show the table headers to me only values
guiiz
guiizOP2w ago
Abmael Souza
Abmael Souza2w ago
how's the model setup for users? como tá a model de usuários?
guiiz
guiizOP2w ago
Br? protected $fillable = [ 'name', 'email', 'password', 'active', 'is_admin', 'is_super' ]; it just the fillable
Abmael Souza
Abmael Souza2w ago
yes, i'm also from brazil
guiiz
guiizOP2w ago
Ah beleza O model tá assim
Abmael Souza
Abmael Souza2w ago
given that the group chat is international, we should stick to english so other people could join and help
guiiz
guiizOP2w ago
Ok
Abmael Souza
Abmael Souza2w ago
we might also add translation below for better expression of ideas is it really a boolean there?
guiiz
guiizOP2w ago
Yes
guiiz
guiizOP2w ago
No description
guiiz
guiizOP2w ago
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_super')->default(false);
});
}
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_super')->default(false);
});
}
the migration
Abmael Souza
Abmael Souza2w ago
I see, if that did not do it, you might be able to add a custom filter in your $table variable on table function
->filters([
Tables\Filters\Filter::make('Exclude Super Users')
->query(fn (Builder $query) => $query->where('is_super', false))
->default(), // Applies by default
])
->filters([
Tables\Filters\Filter::make('Exclude Super Users')
->query(fn (Builder $query) => $query->where('is_super', false))
->default(), // Applies by default
])
it would look like this for your code
$form
->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->placeholder('John Doe'),
Forms\Components\TextInput::make('email')
->label('Email')
->required()
->placeholder('[email protected]'),
Forms\Components\TextInput::make('password')
->label('Password')
->required()
->placeholder('********')
->password()
->autocomplete('new-password'),
Forms\Components\Toggle::make('active')
->label('Ativo')
->default(true),
Forms\Components\Toggle::make('is_admin')
->label('Admin')
->default(false)
->visible(fn (?Model $record) => $record?->id !== Auth::id()),

// Add this component for the is_super condition
Forms\Components\Toggle::make('is_super')
->label('Super User')
->default(false)
->hidden(fn (?Model $record) => $record?->is_super ?? false), // Hides the field for super users
]);
$form
->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->placeholder('John Doe'),
Forms\Components\TextInput::make('email')
->label('Email')
->required()
->placeholder('[email protected]'),
Forms\Components\TextInput::make('password')
->label('Password')
->required()
->placeholder('********')
->password()
->autocomplete('new-password'),
Forms\Components\Toggle::make('active')
->label('Ativo')
->default(true),
Forms\Components\Toggle::make('is_admin')
->label('Admin')
->default(false)
->visible(fn (?Model $record) => $record?->id !== Auth::id()),

// Add this component for the is_super condition
Forms\Components\Toggle::make('is_super')
->label('Super User')
->default(false)
->hidden(fn (?Model $record) => $record?->is_super ?? false), // Hides the field for super users
]);
its a hidden toggle, i bet its not the best option for it, but it might solve your problem the presented solutions were of your approval?
Expecto Patronum
Apply this query in your resource public static function getEloquentQuery(): Builder { return static::getModel()::query()->where(‘is_super’, 0); }

Did you find this page helpful?