So in this case I am working with laravel but it might just be a MySQL "issue". I have a simple database table with only a name
field that has two records:
1: B-Front
2: Frontliner
I have a string called: B-Frontliner
I am trying to get the results from the database, based on my string. In my opinion, I should get both results from my database with the following query:
$results = Artist::where('name', 'LIKE', '%' . $string . '%')->get();
But I am not getting any results. Even with using strtolower
etc, no results. Also not when simply putting everything in lowercase manually, for testing purposes.
Am I misunderstanding all this, or is my query simply wrong?
2
Answers
No, you won’t get any results because neither of the rows in the database contains the text
B-Frontliner
(in either all or part of the field). You’re asking it:None of them do.
By contrast, entering
Front
as the search term would return both results because that text does occur somewhere within the name field in both rows.And searching for, say,
B-Front
would return the first row. Or searching forliner
would return the second row, for example.Your condition is backwards. You want the following comparison:
You need to use the
name
column in the table to create the patterns, not the string the user gave.I’m not sure how to translate this into the ORM syntax you’re using. You may need to use a raw query instead.