Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel binding replace question mark from variable

I have area table like :

-----------------------------
id | name        | level
-----------------------------
1  | India       | country
2  | Some?thing  | country

in this table i have added a row with question mark and i want to select that row as follow Query in eloquent :

Area::select(*)->where("name","LIKE", "%Some?thing%")
               ->where("level","=","country")->get();

but this not give the result because question mark in string in where condition replaced with bindings

the raw sql generated is :

select * from area where name like %Somecountrything% AND level = ?

but i want it like

select * from area where name like %Some?thing% AND level = country
like image 305
Arjun Avatar asked Mar 10 '17 07:03

Arjun


3 Answers

Try this :

Area::whereRaw("name LIKE '%Some?thing%'")
               ->where("level","=","country")->get();

You can inject raw mysql queries to laravel with the help of whereRaw()

like image 69
Muthu17 Avatar answered Oct 27 '22 11:10

Muthu17


Use whereRaw() for this

like

DB::table('users')->whereRaw("email LIKE '%Some?thing%'")->get();
Print_r(DB::getQueryLog());

It output like this:

select * from `abc_users` where email LIKE '%Some?thing%'

Hope this helps!

like image 21
Raunak Gupta Avatar answered Oct 27 '22 11:10

Raunak Gupta


try...

Area::select('*') ....

Full Code:

Client::select('*')->where("name","LIKE", "%xyz?xyz%")
               ->where("city","=","331")->get();

Output: enter image description here

like image 31
Dr.Tricker Avatar answered Oct 27 '22 11:10

Dr.Tricker