php - Laravel MySQL 使用 eloquent 应用产品过滤器(正确的查询是什么?)

标签 php mysql laravel eloquent

我正在尝试用 eloquent 过滤我的产品,但我的过滤器 where 语句永远不会像我目前的方式工作。

我的数据库表如下所示:

产品

+---------------+
| id | name     |
+---------------+
| 1  | product1 | 
| 2  | product2 |
| 3  | product3 | 
| 4  | product4 |
+---------------+

属性

+------------------------------------+
| id | property_group_id | value(int)|
+------------------------------------|
| 1  | 1                 | 20        |
| 2  | 1                 | 10        |
| 3  | 2                 | 2         |
| 4  | 2                 | 4         |
+------------------------------------+

products_properties

+--------------------------+
| product_id | property_id | 
+--------------------------|
| 1          | 1           |
| 1          | 3           |
| 2          | 2           |
| 2          | 4           |
+--------------------------+

我当前使用 Eloquent 生成的 SQL 如下所示:

select * from `products` 
    where exists (
        select * from `properties`
            inner join `products_properties` 
                on `properties`.`id` = `products_properties`.`property_id` 
            where `products`.`id` = `products_properties`.`product_id` and 
            (
                `properties`.`property_group_id` = 1 and <--- property_group_id is not 
                `properties`.`value` >= 15 and                1 and 2 at the same time
                `properties`.`value` <= 25
            ) 
            and 
            (
                `properties`.`property_group_id` = 2 and
                `properties`.`value` >= 1 and 
                `properties`.`value` <= 2
            )
    )

我正在使用此查询查找 product1,但这永远不会发生,因为 property_group_id 在同一行不匹配。在 2 个 where 语句之间使用 OR 也不起作用,因为只有其中 1 个语句必须为 true 才能找到某些内容。

SQL 在 Eloquent 中是这样生成的:

$products = Product::with(['properties' => function($query){
        $query->with('propertyGroup');
    }])
    ->whereHas('properties', function ($query) {
        // Use filter when filter params are passed
        if(array_key_exists('filterGroupIds', $this->filter) && count($this->filter['filterGroupIds']) > 0){

            // Loop through filters
            foreach($this->filter['filterGroupIds'] as $filter){
                // Add where for each filter
                $query->where(
                    [
                        ["properties.property_group_id", "=", $filter['id']], // 1 or 2
                        ["properties.value", ">=", $filter['min']], // 15 or 1
                        ["properties.value", "<=", $filter['max']]  // 1 or 2
                    ]
                );
            }
        }

    })
    ->get();

获得正确结果的正确查询是什么?如果可能的话,我的 Eloquent 代码将如何生成此查询?

最佳答案

每个 $filter 使用一个 whereHas() 子句:

$products = Product::with('properties.propertyGroup');
if(array_key_exists('filterGroupIds', $this->filter) && count($this->filter['filterGroupIds']) > 0) {
    foreach($this->filter['filterGroupIds'] as $filter) {
        $products->whereHas('properties', function ($query) {
            $query->where([...]);
        });
    }
}

关于php - Laravel MySQL 使用 eloquent 应用产品过滤器(正确的查询是什么?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51672244/

相关文章:

PHP 存储过程参数

php - 上传中 Mimetypes 的可靠性 (PHP)

php - 优化 SQL 查询

php - 将中东语言的 doc 和 docx 文件批量转换为 utf-8 编码的 txt

php - Laravel 中如何从数组中获取值

laravel - 查询created_at日期仅等于laravel中的carbon::now()日期

php - Laravel中如何将一个函数的数据访问到另一个函数

javascript - get_browser 不适用于 PHP 中的 IF 语句

php - 使用 PHP SQL 在数据库中查找特定值

php - 为什么我的代码没有从我的数据库中发布数据?