php - Laravel Eloquent : how to filter multiple and/or criteria single table

标签 php database laravel filter eloquent

我正在制作一个与房地产相关的应用程序,我一直很难弄清楚如何设置查询,以便它返回“仅选定区域内的公寓或复式公寓”,我希望用户成为能够在城市的多个选定象限中找到多种类型的属性(property)。

我有一个数据库,其中“类型”列是“公寓”、“房屋”、“复式”、“移动”

在另一列中,我有 quadrant_main 的值:“NW”、“SW”、“NE”、“SE”。

我的代码在只选择一个象限时有效,但是当我选择多个象限时,我得到的结果似乎包括第二、第三或第四象限的所有属性类型,而不仅仅是“公寓”和“复式” “或用户选择的任何类型...任何帮助将不胜感激!提前致谢。

我的 Controller 函数如下所示:

public function quadrants()
{
    $input = \Request::all();
    $currentPage = null;
    $column = "price";
    $order = "desc";

    //
    //  Looks like the input is like 0 => { key: value } ...
    //  (an Array of key/value pairs)

    $q = Listing::where('status','=','Active')->where(function($query) {
        $input = \Request::all();
        $currentPage = null;
        $typeCount = 0;
        $quadrantCount = 0;

        foreach( $input as $index => $object ) {

            $tempObj = json_decode($object);
            $key = key((array)$tempObj);
            $val = current((array)$tempObj);

            if ( $key == "type" ) {
                if ( $typeCount > 0 ) {
                    $query->orWhere('type', '=', $val );
                }
                else {
                    $query->where('type', '=', $val );
                    $typeCount++;
                }
            }
            if ( $key == "quadrant_main" ) {
                if ( $quadrantCount > 0 ) {
                    $query->orWhere('quadrant_main', '=', $val );
                }
                else {
                    $query->where('quadrant_main', '=', $val );
                    $quadrantCount++;
                }
            }
            // else {
            //  $query->orWhere($key,$val);
            // }
        }

        if( $currentPage ) {
            //Force Current Page to Page of Val
            Paginator::currentPageResolver(function() use ($currentPage) {
                return $currentPage;
            });
        }
    });

    $listings = $q->paginate(10);

    return $listings;

最佳答案

看着你的问题,它有点令人困惑,并没有给出明确的答案。您遇到问题的可能原因可能是数据库中的错误数据,或者可能是用户输入的错误。

Disclaimer: Please note that chances are my answer will not work for you at all. In that case please provide more information and we will work things out.

我认为您忽略了一件事,因此您得到了错误的结果。首先让我假设一些事情。

我认为示例用户输入应该如下所示:

array(
    0: '{type: Apartment}',
    1: '{type: Duplex}',
    2: '{quadrant_main: NW}',
    3: '{quadrant_main: SW}',
)

用户的意思是给我任何属于西北或西南地区的公寓或复式公寓。

所以在你的循环结束后,最终的 SQL 语句应该是这样的:

Oh and while we are at SQL topic, you can also log the actual generated SQL query in laravel so you can actually see what was the final SQL getting generated. If you can post it here, it would help a lot. Look here.

select * from listings where status = 'Active' and (type = 'Apartment' or type = 'Duplex' and quadrant_main = 'NW' or quadrant_main = 'SW');

这个查询实际产生的是:

Select any listing which is active and:
1. Type is an apartment, or,
2. Type is a duplex, or,
3. Quadrant is SW, and,
4. Quadrant is NW

假设你有这样一个数据库:

id|type|quadrant_main
=====================
1|Apartment|NW
2|Apartment|SW
3|Apartment|NE
4|Apartment|SE
5|Duplex|NW
6|Duplex|SW
7|Duplex|NE
8|Duplex|SE
9|House|NW
10|House|SW
11|House|NE
12|House|SE

您只会在结果集中收到 1 和 5。这个结果集显然是错误的,而且它依赖于 NW 因为那是 and 条件。

正确的 SQL 查询应该是:

select * from listings where status = 'Active' and (type = 'Apartment' or type = 'Duplex') and (quadrant_main = 'NW' or quadrant_main = 'SW');

因此构建您的 L5 应用程序,使其生成这种 SQL 查询。不要试图将所有内容都塞进一个循环中,而是要有两个循环。一个循环应该只处理 type,另一个循环应该只处理 quadrant_main。这样您就可以在正确的位置获得必要的 条件。


作为旁注:

  1. 切勿直接使用用户输入。始终先对其进行 sanitizer 。
  2. 将所有逻辑都放在 Controller 中并不是最佳做法。使用存储库模式。 See here.
  3. 多个 where 子句通常通过 Criteria 应用。在上面的链接存储库模式中检查。
  4. 您的代码逻辑非常复杂,完全没有必要。无需发送 JSON 对象,只需发送复选框的状态即可。不要试图通过循环来概括函数。相反,一个一个地处理所有复选框,即是否选择了“公寓”,如果是,请将其添加到您的子句中,如果不是,则不要添加。

关于php - Laravel Eloquent : how to filter multiple and/or criteria single table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376882/

相关文章:

mysql - NAVICAT - 我无法数据传输我的存储过程和 View

sql - 如何获取某个用户拥有同一组好友的uid? (SQL)

php - Lumen MySQL 查询未按预期处理 UTF8 值

Laravel eloquent - 多对多,选择只匹配多表

php - 从数据库表中仅获取选定的列 laravel 5 eloqouent

php - 作业跟踪的批处理通知

php - 如何在WordPress中建立MySQL数据库连接?

用于检测分页的 PHP 正则表达式

php - 无法插入 : A foreign key constraint fails

SQL查询源代码