Home > Software engineering >  Laravel orm - # in LIKE where make different results
Laravel orm - # in LIKE where make different results

Time:12-06

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?

CodePudding user response:

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'."%");

CodePudding user response:

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.

  • Related