FilamentF
Filament2y ago
Pan

Searchable either name or barcode

What I am trying to do:
In my relationship manager, I have an action have a form field wherein I can type either the name or barcode of an equipment record and they show the same record with the same label, I want it to still display the name even if I searched the barcode

What I did:
I tried using this
https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results
wherein I can indeed search by typing the barcode and it labels with the name
but I can't search by simply searching the name. Probably because the Equipment::where('barcode')

I tried adding where('barcode','name') but I get an error SQLSTATE[42000]: Syntax error

My issue/the error:
I am unaware if its possible to do search both columns in a single field and how to do that.
I am amateur when it comes to php


Code:
->getSearchResultsUsing(fn (string $search): array => Equipment::where('barcode', 'like', "%{$search}%")->limit(50)->pluck('name','id')->toArray())
                            ->getOptionLabelUsing(fn ($value): ?string => Equipment::find($value)?->barcode),
Solution
Your initial approach was a good start, it was just missing the orWhere

This should search name and barcode, but only show name in the list.

->searchable()
->getSearchResultsUsing(fn (string $search): array => Equipment::query()
  ->where('name', 'like', "%{$search}%")
  ->orWhere('barcode', 'like', "%{$search}%")
  ->limit(50)
  ->pluck('name','id')
  ->toArray()
)
Was this page helpful?