FilamentF
Filament10mo ago
Hedi

Select createOptionUsing shows the record's ID and not the text

hello, i have a Select with create option, here's the code
public function selectCustomerForm($form): Form
{
    return $form->schema([
        Select::make('customer_id')
            ->label('')
            ->translateLabel()
            ->live()
            ->options(function () {
                return OctaneCustomer::where('user_id', Auth::id())
                    ->limit(5)
                    ->pluck('name', 'id')
                    ->toArray();
            })
            ->getSearchResultsUsing(function (string $search) {
                return OctaneCustomer::where('user_id', Auth::id())
                    ->where('user_id', Auth::id())
                    ->limit(5)
                    ->pluck('name', 'id')
                    ->toArray();
            })
            ->createOptionForm(OctaneCustomerResource::getFormColumns())
            ->createOptionUsing(function ($data): int {
                $record = OctaneCustomer::create(['name' => $data['name'], 'mobile' => $data['mobile'], 'user_id' => Auth::id()]);
                return $record->getKey();
                //tried 
return $record->id;
, same problem
            })
            ->searchable(),
    ])->statePath('customerData');
}

this is inside a custom page that contains multiple forms, here's more info on the class and the customerData
class POS extends Page implements HasForms, HasTable
{
    use InteractsWithForms;

    public ?array $customerData = ['customer_id' => null];
    //...more irrelevant functions/variables
protected function getForms(): array
{
    return [
        'selectCustomerForm',
    ];
}
}

and this is how i show the form in the blade file
{{ $this->selectCustomerForm }}

i don't have this problem using createOptionUsing on other pages where i use the traditional Resource page.
everything works correctly, record is created, customer_id is correct too, the only problem is this bad UX behavior.
Was this page helpful?