skip to Main Content

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


  1. 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:

    "Show me all the artists whose name contains the text B-Frontliner"

    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 for liner would return the second row, for example.

    Login or Signup to reply.
  2. Your condition is backwards. You want the following comparison:

    WHERE 'B-Frontliner' LIKE CONCAT('%', name, '%')
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search