I have a Filament form which includes a searchable customer Select box, as well as other fields, that works fine manually.
I want to fill() the form with data on occasions so I use fill() to provide the customer ID on load.
This appears to work as the correct data is in the form but the select box shows the ID and not the associated customer name.
If I pass customer_id = 1 I get the a 1 in the searchable select box.
The attached image just shows the 1 in the select box.(https://i.sstatic.net/EqTPHYZP.png)
It makes sense that the customer is not shown as it is a search box and I haven’t searched, I have provided the customer ID but is there a way round this to show the customer I have provided the ID for?
Many thanks
Mike
Minimal section of my resource file
class TempResource extends Resource
{
protected static ?string $model = Order::class;
public static function form(Form $form): Form
{
return $form
->schema([
Group::make()
->schema(
[
Select::make('customer_id')
->getSearchResultsUsing(fn (string $search): array => Customer::where('name', 'like', "%{$search}%")
->limit(50)->pluck('name', 'id')->toArray())
->searchable()
->live(),
]),
]);
}
}
Minimal section of my create page file
class CreateTemp extends CreateRecord
{
protected function fillForm(): void
{
parent::fillForm();
$request = request();
$data = [];
if (! empty($request->get('customer_id'))) {
$data['customer_id'] = $request->get('customer_id');
}
$this->form->fill($data);
}
}
I have tried providing the customer collection to the fill() statement as well as an array with ID => customerName. Both error which is understandable.
I would expect (like) to have the select box populated with Customer that I have supplied the ID for using the fill() command.
Mike Cox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think I’ve solved my own problem!
I think the problem is with the database lookup and search. I changed it from
Select::make('customer_id')
->getSearchResultsUsing(fn (string $search): array => Customer::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->searchable()
->live(),
to
Select::make('customer_id')
->relationship(name: 'customer', titleAttribute: 'name')
->searchable(['name', 'reference', 'email', 'address'])
->live(),
This seems to work as expected.
Mike Cox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.