Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel adding parameter with raw queries using Like operators and '%"

Tags:

mysql

laravel

I did some ways to assign parameter but it's not working

Here is my code:

$sqlDefault = "Select * from histories as h where h.status like % :status %";

return DB::select(DB::raw($sqlDefault), ['status' => $status]);

It's not working so I try another ways to assign:

'%:status%' or '%':status'%'

But it's not work

Now I don't use parameter and it's worked perfectlly

$sqlDefault = "Select * from histories as h where h.status like '%$status%'";

The question is how I can use parameter when using like operator with "%" ?

like image 464
Vu Hoang Duc Avatar asked Dec 24 '22 15:12

Vu Hoang Duc


1 Answers

Replace $status with '%'.$status.'%'

$sqlDefault = "Select * from histories as h where h.status like  :status ";

return DB::select(DB::raw($sqlDefault,['status' => '%'.$status.'%']));

or just use select only:

return  = DB::select(sqlDefault , ['status' => '%'.$status.'%']);
like image 111
Govind Samrow Avatar answered May 20 '23 00:05

Govind Samrow