skip to Main Content

I want to get records that their titles contains "hello #12", so I used this query:

Post::query()->where('title', 'LIKE', "%hello #12%");

Problem is it returns some records with titles like this:
"hello #15", "hello you" and …

Is # have special use in mysql or laravel query?

How can I get the result I wanted?

2

Answers


  1. I think what’s happening is that when you’re querying like that, you are actually escaping the character #. (Double Quotes)

    So, instead try this way:

    Post::query()->where('title', 'LIKE', "%".'hello #12'."%");
    
    Login or Signup to reply.
  2. I tested your problem and got proper results by using the following query syntax (Model Facade):

    $results = Post::where('title', 'LIKE','%hello #12%')->get();
    

    Extra: if you dd() your query, im my case:

    dd($results = Post::where('title', 'LIKE','%hello #12%'))
    

    you´ll see, that the ‘#’ character is interpreted as inteded as part of the full query string.

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