User select

Hello, I'm starting to use FilamentPHP and would like to ask a question. How do I limit the user select to display only those with the role of realtor? I use this plugin: https://v2.filamentphp.com/plugins/althinect-spatie-roles-permissions
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('user_id')
->relationship('user', 'name')
->searchable()
->label(__('Realtor'))
->required(),
Forms\Components\TextInput::make('title')
->required()
->maxLength(255),
Forms\Components\Textarea::make('description')
->columnSpanFull(),
Forms\Components\TextInput::make('status')
->required(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('user_id')
->relationship('user', 'name')
->searchable()
->label(__('Realtor'))
->required(),
Forms\Components\TextInput::make('title')
->required()
->maxLength(255),
Forms\Components\Textarea::make('description')
->columnSpanFull(),
Forms\Components\TextInput::make('status')
->required(),
]);
}
Filament
Spatie Roles Permissions by Tharinda Rodrigo - Plugins - Filament
Resources for Roles and Permissions. Generate Permissions based on Models and Resources. Generate Policies. Manage Permissions for teams.
3 Replies
toeknee
toeknee3mo ago
https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
->relationship(name: 'user', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => $query->where('your_query_here'))
->relationship(name: 'user', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => $query->where('your_query_here'))
MK | Programmer
MK | ProgrammerOP3mo ago
I managed to do it with this:
->relationship(name: 'user', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => $query->whereHas('roles', fn (Builder $query) => $query->where('name', 'realtor')))
->relationship(name: 'user', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => $query->whereHas('roles', fn (Builder $query) => $query->where('name', 'realtor')))
toeknee
toeknee3mo ago
That does it,. I think you can do: ->role('Realtor') without the where because of the RolesTrait
Want results from more Discord servers?
Add your server