F
Filament10mo 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),
->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. ```php...
Jump to solution
4 Replies
Dennis Koch
Dennis Koch10mo ago
I tried adding where('barcode','name') but I get an error SQLSTATE[42000]: Syntax error
Share the error, if you want help on that.
I am unaware if its possible to do search both columns in a single field and how to do that.
This would be just an added ->orWhere() right? You should wrap both in a ->where() condition though to not affect other conditions
Pan
PanOP10mo ago
Hi, I am not sure if I am implementing it correctly to what I want to happen. Reading through https://laravel.com/docs/10.x/queries#or-where-clauses I think I phrased my issue without combining it with what I am trying to do. Sorry about that. I wanted to be able to search in my Action with a form wherein the search results shows only the name but if I type/searched in the barcode, it still shows the name and it is the record that matches with the barcode. Currently my band aid solution is this
->options(Equipment::query()
->select([
DB::raw("CONCAT(name, ' ', barcode) as namecode"), 'barcode',
])
->whereNull('user_id')
->pluck('namecode','barcode')
->toArray())
->searchable()
->options(Equipment::query()
->select([
DB::raw("CONCAT(name, ' ', barcode) as namecode"), 'barcode',
])
->whereNull('user_id')
->pluck('namecode','barcode')
->toArray())
->searchable()
Here in the image, what's currently happening But What I want is if I type "12", only the name shows as the options like "Key #103", "Key #201", "Solar Panel"... which corresponds to their barcode# while also maintaining that if I simply search "Solar Panel" it'll still show up as usual. I am still learning PHP so I misinterpret please enlighten me. Thank you!
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
No description
Solution
Dennis Koch
Dennis Koch10mo ago
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()
)
->searchable()
->getSearchResultsUsing(fn (string $search): array => Equipment::query()
->where('name', 'like', "%{$search}%")
->orWhere('barcode', 'like', "%{$search}%")
->limit(50)
->pluck('name','id')
->toArray()
)
Pan
PanOP10mo ago
Thank you so much, its working and showing the names only even if I search the barcode! I am not sure if I could still ask about wherein after inputting a selected option, the name changes into their id when I start searching another option. I tried adding ->getOptionLabelUsing(fn ($value): ?string => Equipment::find($value)?->name) if it could work but didn't, I'm not sure if this is the solution as I get an error Call to undefined method Illuminate\Database\Eloquent\Builder::getOptionLabelUsing() But you still gave me the solution I was looking for in the post. Thanks !!
No description
No description
No description
Want results from more Discord servers?
Add your server