-3

Laravel "like" query doesn't work. For example, the query is as follows;

 $result = Sports::where('name', 'LIKE', '%'.$name.'%')

for example

$name= 'football';

If I use

$result->toSql();

It says

select * from sports`where ( `name` like ? )

It should be

select * from sports where (namelike '%football%' ) .

As you see , I can't see my item(football) in like query and only ?. I'm using Laravel version 4.2. What's wrong in query? and How can I fix it? Thanks for your time.

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48

2 Answers2

0

The ?s are just placeholders used parameterize the query. It helps prevent SQL injection. When the query is executed, they get replaced with the parameters you gave.

If you want to see the entire query, I'd recommend using DB::getQueryLog(). It'll show you an array with the query and the parameters used (in the bindings section of the array). Here's a reference to a similar question:

https://stackoverflow.com/a/18236656/1193304

Community
  • 1
  • 1
Chris
  • 4,277
  • 7
  • 40
  • 55
0

if you are using 4.2, try out following code:-

$keyword= Input::get('keyword');

        return View::make('sports.search')
        ->with('result', Sports::where('name', 'LIKE', '%'.$keyword.'%')->get())
        ->with('keyword', $keyword);
Anup GC
  • 742
  • 1
  • 6
  • 16