phplaravellaravel-filamentfilamentphp

How can I restrict Filament select field options to the ones explicitly associated with the logged in user?


In my application, users can add their residential properties and create a listing based on that listing. Users can have many

    public function property(): BelongsTo
    {
        return $this->belongsTo(Property::class);
    }
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

   public function listing(): HasMany
    {
        return $this->hasMany(Listing::class);
    }
            Select::make('property_id')
                        ->relationship('property', 'house_no')
                        ->label('Stand Number')
                        ->createOptionForm([
                            Section::make('Details')->schema([
                                Select::make('area')
                                    ->options([
                                        "Bloomingdale" => "Bloomingdale",
                                        'Ashdown Park' => "Ashdown Park"
                                    ]),
                                TextInput::make('house_no')
                                    ->required()
                                    ->label('House Number')
                                    ->numeric(),
                                TextInput::make('street')
                                    ->nullable(),
                                TextInput::make('families')
                                    ->label('Families On The Property')
                                    ->required()
                                    ->numeric(),
                            ]),

My current setup of this field allows users that are not associated with the property to select it and associate it with their listing.

I want users' property options to be limited to the ones they added. How do I do that?


Solution

  • You can filter the option on the relationship by using modifyQueryUsing parameter (3rd parameter)

    ->relationship('property', 'house_no', fn(Builder $query) => $query
        ->where('user_id', auth()?->id())
    )