Laravel Eloquent Ignore Casing

倾然丶 夕夏残阳落幕 提交于 2019-11-29 09:19:27

Use whereRaw with parameter binding to sanitize your whereRaw statement:

$term = strtolower($vars['language']);
Item::whereRaw('lower(language) like (?)',["%{$term}%"])->get();

Prev answer In some dabases you can use operator ilike in your where. For example

Item::where('language', 'ilike', $vars['language'])->get();

All available operators are:

protected $operators = array(
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'not like', 'between', 'ilike',
    '&', '|', '^', '<<', '>>',
);

Edit: ilike is case-insensitive like.

As per this post, a db-independent way to solve this would be:

Item::whereRaw('LOWER(language) = ?', [$vars['language']])->get();

The filter() collection method can be useful if you are trying to find a laravel-based solution :

// $this refers to an instance of an eloquent User Model
$name = 'BiLbo baGGings';    
return $this->contactInformation->filter(function($value, $key) use($name) { 
   if(strtolower($value->name) == strtolower($name)) {
       return $value;
   }
 });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!