FilamentF
Filament12mo ago
rabol

Strange new error in select component

Hi I have had the same resource for a few weeks and it has been working without issues, but this morning I got a strange error.

I have created separate form for the 'CreatePage like this:

public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                Forms\Components\Select::make('exchange_info_id')
                    ->searchable()
                    ->relationship('exchangeInfo'),
            ]);
    }


there is 1322 records in the exchangeInfo, which is why I cannot use a 'simple' select

The form renders correctly, when I try to search for an entry I get this error:

foreach() argument must be of type array|object, null given
Solution
It must have been something with the relation.

I have changed the select to this and now it works:

Forms\Components\Select::make("exchange_info_id")
                ->searchable()
                ->getSearchResultsUsing(
                    fn(string $search): array => ExchangeInfo::where(
                        "symbol",
                        "like",
                        "%{$search}%"
                    )
                        ->limit(50)
                        ->pluck("symbol", "id")
                        ->toArray()
                )
                ->getOptionLabelUsing(
                    fn($value): ?string => ExchangeInfo::find($value)?->symbol
                ),
Was this page helpful?