Laravel集合搜索不区分大小写 [英] Laravel collection search case insensitive

查看:73
本文介绍了Laravel集合搜索不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索集合,因此我希望此搜索大小写不敏感,或者至少更改小写形式的集合值.我该怎么办?

I am searching through a collection and I would like to have this search case insensitive or at least change the collections values in lowercase. How can I do that?

$value = strtolower($value);
$collection = $collection->where($attribute, $value);

$ value是小写字母,而集合中的内容不是小写字母,因此没有匹配项.

$value is lowercase while the content in the collection not, so there are no matches.

推荐答案

您可以改为将 filter()方法与满足您需要的回调一起使用:

You could instead use the filter() method with a callback that does what you want:

$collection = $collection->filter(function ($item) use ($attribute, $value) {
    return strtolower($item[$attribute]) == strtolower($value);
});

这篇关于Laravel集合搜索不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆