WhereRaw Laravel with variable

混江龙づ霸主 提交于 2019-12-24 13:28:44

问题


I'm trying to do this query but I got an error (Malformed UTF-8 characters, possibly incorrectly encoded):

DB::table('my_table')->select(DB::raw("id"))
    ->whereRaw('UPPER(name)','=', $upper_name)
    ->pluck('id')->first();

I'm trying to add the UPPER sql function to the query. With direct sql, the query should be:

select * from my_table
where UPPER(name) = 'HELLO'

Where $upper_name = HELLO.


回答1:


Simpliest Way to do that. Hope will help you

DB::table('my_table')->select('id')
    ->where(DB::raw("UCASE(name)"), $upper_name) 
    ->first();

UCASE - Convert the text to upper-case




回答2:


Bah... I got it:

DB::table('my_table')->select(DB::raw("id"))
    ->whereRaw('UPPER(name) = ?', $upper_name)
    ->pluck('id')->first();


来源:https://stackoverflow.com/questions/55384313/whereraw-laravel-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!