简体   繁体   English

如何使用 Laravel 查询构建器在 Postgresql 数据库中搜索不区分大小写的查询?

[英]How to search incasesensitive query in the Postgresql database using Laravel query builder?

I want to create a filter.我想创建一个过滤器。 But I am facing case-sensitive problem.但我面临区分大小写的问题。 I have tried with my below code.我试过下面的代码。 But It didn't work.但它没有用。 I have got error 'call undfiend lcase'.我有错误“调用 undfiend lcase”。

 $type = strtolower($type);
 $name = strtolower($name);
 $users  = DB::table('daftar.bank_list AS uc')
     ->leftJoin('users.users as u','u.bank_id','=','uc.id')
     ->when($type, function ($query, $type) {
         return $query->where('LCASE'('uc.type'), $type);
     })
     ->when($name, function ($query, $name) {
         return $query->where('LCASE'('uc.name'), $name);
     })
     ->get();

My database is Postgresql.我的数据库是 Postgresql。

Postgres uses the LOWER() function, not LCASE . Postgres 使用LOWER()函数,而不是LCASE Also, if you want to call a native Postgres function, you should be using whereRaw :另外,如果你想调用原生 Postgres 函数,你应该使用whereRaw

$users = DB::table('daftar.bank_list AS uc')
    ->leftJoin('users.users as u', 'u.bank_id', '=', 'uc.id')
    ->when($type, function ($query, $type) {
        return $query->whereRaw('LOWER(uc.type) = ?', [$type]);
    })
    ->when($name, function ($query, $name) {
        return $query->whereRaw('LOWER(uc.name) = ?', [$name]
    })
    ->get();

Try this:尝试这个:

$type = strtolower($type);
$name = strtolower($name);
$users  = DB::table('daftar.bank_list AS uc')
    ->leftJoin('users.users as u','u.bank_id','=','uc.id')
    ->when($type, function ($query, $type) {
        return $query->where('uc.type','ILIKE', '%'.$type.'%');
    })
    ->when($name, function ($query, $name) {
        return $query->where('uc.name','ILIKE', '%'.$name.'%');
    })
    ->get(); 

You can search by ILIKE query.您可以通过ILIKE查询进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM