【问题标题】:Laravel 7 / SQL case insensitive where clauseLaravel 7 / SQL不区分大小写的where子句
【发布时间】:2020-12-06 14:09:23
【问题描述】:

从数据库获取数据时遇到问题。 问题是,搜索查询使用区分大小写的参数,这是我不想要的。

需要一个解决方案,查找所有数据库字段,不区分大小写,并且与字符串完全相同,前后均无文本。

例子:

首先我得到数据:$affiliateOpens = AffiliateOpen::where('user_id', $user->id)->get();

然后,我将要搜索的字符串转换为小写,就像这样(因为有许多不同字符大小写的记录:

$sources = [];
    foreach ($affiliateOpensCollection  as $affiliateOpen)
        $sources[] = strtolower($affiliateOpen->source);

 foreach ($uniqueSources as $source) {
        $sourceData[$source]['installs'] = count($affiliatesOpenCollection->where('source', $source)->where('date_installed', '<>', null));
 }

如何在不区分大小写的情况下进行搜索并从数据库中获取数据?

【问题讨论】:

    标签: php sql laravel laravel-5 eloquent


    【解决方案1】:

    请尝试:

       $sourceData[$source]['installs'] = count($affiliatesOpenCollection->filter(function ($item) use ($source) {
                return (strtolower($item['source']) == strtolower($source) && $item['date_installed'] != null);
            }));
    

    【讨论】:

    • 谢谢,但我收到以下错误:Method Illuminate\Database\Eloquent\Collection::whereRaw 不存在。
    猜你喜欢
    • 2016-09-03
    • 2021-02-27
    • 2014-10-19
    • 2016-11-26
    • 2017-06-21
    • 1970-01-01
    • 2011-10-16
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多