I want to search for an exact phrase from the "customers" table

Hi all, I want to search for an exact phrase, for example "John Doe" from the "customers" table However, the default query is
select * from `customers` where ((`name` like '%John%')) and ((`name` like '%Doe%'))
select * from `customers` where ((`name` like '%John%')) and ((`name` like '%Doe%'))
I want the query to have: where name like '%John Doe%' The problem is in this method extractTableSearchWords()
5 Replies
vothanh4231
vothanh4231OP14mo ago
It doesn't work as I expected, it's the same as before
signetplanet
signetplanet14mo ago
I was looking at this earlier, and I noticed the same thing in that extractTableSearchWords() doesn't allow for quotes. So putting in "firstname lastname" for example will have it return an array of two values "firstname and lastname". I think this could be quickly resolved by changing the line of code in the extractTableSearchWords() method from
return explode(' ', preg_replace('/\s+/', ' ', $search));
return explode(' ', preg_replace('/\s+/', ' ', $search));
to:
return str_getcsv(preg_replace('/\s+/', ' ', $search), ' ');
return str_getcsv(preg_replace('/\s+/', ' ', $search), ' ');
I was planning on starting a discussion and making a merge request but I'm still checking out the library and haven't committed to it yet. *Note: I didn't test any of this, so if you are going to do a merge request, I expect it will require testing. Forgot to include your name in the reply so perhaps you didn't see my response above. Just wanted to make sure you got it.
cheesegrits
cheesegrits14mo ago
The only thing I can think of that you might need to be careful of is that str_getcsv() will return an array with a null entry, where explode() with return an array with an empty string, if passed an empty string. But I think everywhere extractTableSearchWords() is called, the code tests that $search is filled before calling it, so should be OK. So as far as I can tell, that should be a safe change, and a worthwhile thing to PR.
signetplanet
signetplanet14mo ago
Got some time to put this together and created the PR. I wrapped it so the [null] will not happen so it should have no compatibility issues from what I've seen and tested. https://github.com/filamentphp/filament/pull/9291
GitHub
feature: Allow table searches to include phrases when wrapped in do...
When searching in tables the search previously broke each word separately on spaces between them. However sometimes that generates an unexpected large number of results, especially when search is e...
Want results from more Discord servers?
Add your server