WhereRaw Laravel 带变量

WhereRaw Laravel with variable

我正在尝试执行此查询,但出现错误(格式错误的 UTF-8 字符,可能编码不正确):

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

我正在尝试将 UPPER sql 函数添加到查询中。使用直接 sql,查询应该是:

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

其中 $upper_name = 你好。

呸……我明白了:

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

最简单的方法。希望能帮到你

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

UCASE - 将文本转换为大写