laravel 如何在 eloquent 模型中搜索不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51497890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search case insensitive in eloquent model
提问by Davit
I wont to search case-insensitive in eloquent model. Now I am used this
我不会在 eloquent 模型中搜索不区分大小写的。现在我习惯了这个
Model::where($column, 'LIKE', '%' . $value . '%' );
But it is case sensitive. How can i solve this??
I also find this post How can I search (case-insensitive) in a column using LIKE wildcard?but I can not use it in eloquent model
但它区分大小写。我该如何解决这个问题??
我还发现这篇文章如何使用 LIKE 通配符在列中搜索(不区分大小写)?但我不能在 eloquent 模型中使用它
采纳答案by Ts8060
I suggest Upper function in this case
在这种情况下,我建议使用 Upper 函数
Model::whereRaw("UPPER('.$column.') LIKE '%'". strtoupper($value)."'%'");
like this
像这样
回答by Randy Dryburgh
Actually, you don't need to use UPPER
, just use ilike
as the comparator and it will do a case-sensitive comparison.
实际上,您不需要使用UPPER
,只需ilike
用作比较器,它就会进行区分大小写的比较。
Model::where('column', 'ilike', '%' . $value . '%')
You doneed the %
signs to signify the substring you're searching for.
您确实需要这些%
符号来表示您正在搜索的子字符串。
回答by guiCunha
What's your collation of tables?
你的表格整理是什么?
This is a particularity of the collation in your database, try using latin_general_cior latin_gerneral_cs, they are the insensitive case.
这是数据库中整理的特殊性,请尝试使用latin_general_ci或latin_gerneral_cs,它们是不敏感的情况。
But if you are trying to get this in utf8_binby example, it will not run because this collation is sensitive.
但是,如果您试图通过示例在utf8_bin 中获取它,它将不会运行,因为此排序规则是敏感的。
All examples are running under MySql.
所有示例都在 MySql 下运行。
回答by Peace Ngara
This has been answered use iLike
operator it's case insensitive see
这已得到回答使用iLike
运算符不区分大小写,请参阅
Laravel Eloquent Ignore Casing
Or for fun do this, not recommended ..
或者为了好玩,不推荐这样做..
// prepare two variants of the same value ?
// first to uppercase
// second to lowercase
$valueUp = strtoupper($value);
$valueLower = strtolower($value);
Model::where($column, 'LIKE', '%' . ($valueUp || $valueLower) . '%' );
Did not test it but that's the idea to win ..
没有测试它,但这是获胜的想法..