Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel query with case statement in where clause append IS NULL after condition

->where([
         'er.id_user_request_to' => $nIdUser
       ])
->where(DB::raw('case WHEN ge.id_user IS NOT NULL THEN ge.id_user = '.$nIdUser.' ELSE 0 END'))

tried to print a query using following code

$qry = vsprintf(str_replace(array('%', '?'), array('%%', '"%s"'), $oEvents->toSql()), $oEvents->getBindings());
dd($qry);

This prints a query like

WHERE
(
    `er`.`id_user_request_to` = "6") 
    AND(
         CASE WHEN ge.id_user IS NOT NULL THEN ge.id_user = 6 ELSE 0 END
       )
) **IS NULL**

Not getting from where it adds IS NULL at the end of the query

like image 994
Ruchita Sheth Avatar asked Jan 01 '23 04:01

Ruchita Sheth


1 Answers

Replace your DB::raw with whereRaw

->where([
     'er.id_user_request_to' => $nIdUser
   ])
->whereRaw(('case WHEN ge.id_user IS NOT NULL THEN ge.id_user = '.$nIdUser.' ELSE 0 END'))

I hope it help's

like image 131
Vipul Prajapati Avatar answered Jan 05 '23 02:01

Vipul Prajapati