Reputation: 43
$a = table::select('name')->where('name', 'LIKE', '%'.$term.'%')->get();
$term holds the value entered by user. My Issue is, the like does not work and it returns all results from the table irrespective of the word "$term".
If I do a query like -
$a = table::select('name')->where('name', 'LIKE', '%abc%')->get();
this works fine, but every time I pass Php variable, it does not work as expected. Can any one enlighten a bit on this.
EDIT --
I found the issue. My $term was not getting the value. Such a foolish issue. Thank You every one who replied.
Upvotes: 2
Views: 311
Reputation: 4224
Even though there is nothing wrong in your sql query
I suggest you to check the value of $term
You may also try this:
$a = table::select("name")->where("name", "LIKE", "% $term %")->get();
OR
$a = table::select("name")->where("name", "LIKE", "% {$term} %")->get();
Upvotes: 1
Reputation: 742
Since query looks correct. This may be it work.
$a = table::select('name')->where('name', 'LIKE', "%.$term.%")->get();
Upvotes: 1
Reputation: 16997
Not sure really whats wrong, it looks fine, you can also make use of sprintf function, like below
$a = table::select('name')->where('name', 'LIKE', sprintf("%%%s%%",$term))->get();
// OR
$a = table::select('name')->where('name', 'LIKE', "%$term%")->get();
Upvotes: 1