How can I use MySQL IF
on a column using Laravel? How can I optimize the following query to use Laravel's model or Query builder? Right now I'm using a raw query.
$contacts = ['88544','22455','66981']
DB::select('SELECT name,
IF(file_path=null OR file_path=\'\',\'\',CONCAT("' . url("/media") . "/\",file_path)) AS profile_pic_url FROM users
LEFT JOIN file_uploads on file_id=file_uploads.id WHERE
(phone_number IN('" . implode("','", $contacts) . "')
OR phone_number_full IN('" . implode("','", $contacts) . "')) ORDER BY name ASC");
try this and see if it works
DB::table('users')
->select('name')
->selectRaw(IF(file_path=null OR file_path=\'\',\'\',CONCAT("' . url("/media") . "/\",file_path)) AS profile_pic_url)
->join('file_uploads', 'file_id','=','file_uploads.id')
->whereRaw(phone_number IN('" . implode("','", $contacts) . "')
OR phone_number_full IN('" . implode("','", $contacts) . "'))
->orderBy('name', 'asc')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.