【问题标题】:Laravel eloquent run a query on a query resultLaravel eloquent 对查询结果运行查询
【发布时间】:2018-10-22 08:19:52
【问题描述】:

我正在使用 Laravel Eloquent 并尝试从我运行的查询中查询我的结果。我花了很多时间在谷歌上搜索,但没有找到任何有帮助的东西。我有一个查询,我称之为$poolResults。如果用户输入一个城市,我想根据匹配的邮政编码或用户输入的城市进一步过滤这些结果。这是我的第二个查询。

如果用户输入$locationOfUser,则过滤$poolResults 找到以下任何一项(邮政编码、城市或城市搜索)。

 if($locationOfUser != ''){
                $poolResults = $poolResults->where('city', $locationOfUser)
                    ->orwhere('users.city_search', $locationOfUser)
                    ->orwhereIn('users.zip', $zipCodes)->get();
            }

结果我得到了这个错误屏幕。

【问题讨论】:

  • 函数区分大小写orWhere(),而不是orwhere()(orwhereIn() 也是如此,应该是orWhereIn()。
  • 您好,尝试将第一个查询的结果存储在关联数组中,然后您的过滤器将过滤数组中的数据。

标签: php laravel eloquent


【解决方案1】:

原来参数分组成功了

DB::table('users')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();

https://laravel.com/docs/5.6/queries#parameter-grouping

【讨论】:

    【解决方案2】:

    这个orwhere 运算符不存在,你可以使用orderBy 或类似的东西。

    【讨论】:

    • orderBy 方法用于找到的集合。他想要或从哪里获得收藏。
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2021-10-26
    • 2019-12-24
    • 2021-05-28
    • 2023-03-07
    • 2019-12-06
    • 1970-01-01
    • 2017-10-22
    相关资源
    最近更新 更多