Show Users with role as User only

So I made a default User and I just added a few users, and my table in Database has a field called role in it and I only want to show the users that have that role field as User, I am not sure how I would do it. Basically is there a way to run this query in Filament "SELECT * FROM users WHERE role='User'"
Solution:
so you can start with ``` return $table ->query(User::query())...
Jump to solution
5 Replies
Lara Zeus
Lara Zeusβ€’12mo ago
where you want to call that query? do you have a full resource or customer table or widget or filter... πŸ™‚ there is ->query() that you can change it to what you like
Billi 🌻
Billi 🌻OPβ€’12mo ago
Basically, like let's say I have the table Users and I made a page with Filament, it is showing me all the Users in my table, I haven't done any query on Laravel end as well, so its a basic default table so I want to query in Filament page, and not show other Admin users
class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')->required(),
TextInput::make('email')->email(),
TextInput::make('password')->password()->visibleOn('create'),
Select::make('role')->options([
'User' => 'User',
'Admin' => 'Admin'
]),
Select::make('status')->options([
'Approved' => 'Approved',
'Pending' => 'Pending'
])->visibleOn('edit'),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')->label('User ID'),
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('role'),
TextColumn::make('status'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')->required(),
TextInput::make('email')->email(),
TextInput::make('password')->password()->visibleOn('create'),
Select::make('role')->options([
'User' => 'User',
'Admin' => 'Admin'
]),
Select::make('status')->options([
'Approved' => 'Approved',
'Pending' => 'Pending'
])->visibleOn('edit'),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')->label('User ID'),
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('role'),
TextColumn::make('status'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Thanks for the reply and sorry I am kind of new to this so I have no idea.
Solution
Lara Zeus
Lara Zeusβ€’12mo ago
so you can start with
return $table
->query(User::query())
return $table
->query(User::query())
there is other options too, like using filters and more check this for more https://filamentphp.com/docs/3.x/panels/resources/listing-records
Lara Zeus
Lara Zeusβ€’12mo ago
and you can do User::query()->where('..','..') but I recommend you give the filters a try πŸ™‚
Billi 🌻
Billi 🌻OPβ€’12mo ago
Yeah I will use Filters for the other things like its basically for Sports, so I will use filters for different sports Thank you so much, it worked!! Really appreciate it!
Want results from more Discord servers?
Add your server