When attempting to query using eloquent with a LIKE statement, the search term gets urldecoded which causes, for example, a '%23' to resolve to another character unexpectedly.
//A common use case for term would be user input from a search form:
//eg: $term = $request->input('search');
$term = '23 test';
$query = User::where('name', 'LIKE', '%' . $term . '%');
dd($query->toSql(), $query->getBindings());
The dd()
will output a dump showing the binding as # test%
instead of %23 test%
A quick workaround is to use $term = urlencode($term);
but this seems like it shouldn't be required. I would expect to be able to search with a LIKE query + % + digits in a Database query without having to first URL encode them.
Can't replicate this in 5.5.
Please upgrade to 5.5 as your version is currently not supported.
Facing the same problem today, you can do
$query = User::where('name', 'LIKE', "%$term%");
dd($query->toSql(), $query->getBindings());
// toSql() print
"select * from `Users` where `name` LIKE ?"
// getBindings() print
array:1 [â–¼
0 => "%yourterm%"
]
This should work.
How about 2 or 3 or more LIKE?
$categories = DB::Where('name', 'like', '%' . Input::get('name') . '%')->get();
Most helpful comment
Facing the same problem today, you can do
This should work.