【问题标题】:conditions in eager loading in eloquent laravel雄辩的laravel中急切加载的条件
【发布时间】:2015-01-29 15:41:17
【问题描述】:

我的产品就是这个代码。我使用 with() 方法来加载变化关系。 我有一些过滤器。但如果不存在任何变体,我会得到 Product。

我怎样才能只获得存在变化的这个产品?

$query = Product::with(array(
            'variations' => function($query) use($filters){
                if(isset($filters['price_start']) && !empty($filters['price_start'])){
                    $query->groupBy('id')->having(DB::raw('SUM(price_base + price_engraving)'), '>=', $filters['price_start']);
                }
                if(isset($filters['price_end']) && !empty($filters['price_end'])){
                    $query->groupBy('id')->having(DB::raw('SUM(price_base + price_engraving)'), '<=', $filters['price_end']);
                }

                if(isset($filters['q_magazine_start']) && !empty($filters['q_magazine_start'])){
                    $query->where('q_magazine', '>', $filters['q_magazine_start']);
                }
                if(isset($filters['q_magazine_end']) && !empty($filters['q_magazine_end'])){
                    $query->where('q_magazine', '<', $filters['q_magazine_end']);
                }

                return $query;
            }
        ))->whereIn('id', $productList);

【问题讨论】:

    标签: php laravel eloquent eager-loading


    【解决方案1】:

    要实现这一点,您必须再次执行几乎相同的操作,但使用 whereHas。由于闭包将完全相同,我们可以将它放在一个变量中以避免重复代码:

    $filterClosure = function($query) use ($filters){
        if(isset($filters['price_start']) && !empty($filters['price_start'])){
            $query->groupBy('id')->having(DB::raw('SUM(price_base + price_engraving)'), '>=', $filters['price_start']);
        }
        if(isset($filters['price_end']) && !empty($filters['price_end'])){
            $query->groupBy('id')->having(DB::raw('SUM(price_base + price_engraving)'), '<=', $filters['price_end']);
        }
    
        if(isset($filters['q_magazine_start']) && !empty($filters['q_magazine_start'])){
            $query->where('q_magazine', '>', $filters['q_magazine_start']);
        }
        if(isset($filters['q_magazine_end']) && !empty($filters['q_magazine_end'])){
            $query->where('q_magazine', '<', $filters['q_magazine_end']);
        }
    };
    
    $query = Product::with(array('variations' => $filterClosure))
                    ->whereHas('variations', $filterClosure)
                    ->whereIn('id', $productList);
    

    (顺便说一句,匿名函数结束时不需要返回$query

    编辑

    我们发现您需要将 groupBy 语句更改为 where

    $filterClosure = function($query) use ($filters){
        if(isset($filters['price_start']) && !empty($filters['price_start'])){
            $query->where(DB::raw('price_base + price_engraving'), '>=', $filters['price_start']);
        }
        if(isset($filters['price_end']) && !empty($filters['price_end'])){
            $query->where(DB::raw('price_base + price_engraving'), '<=', $filters['price_end']);
        }
    
        // same code as above
    };
    

    【讨论】:

    • 现在我收到这些错误“子查询返回超过 1 行”
    • 您有更多详细信息吗?仅出于测试目的,您能否快速注释掉whereHas() 部分,看看它是否运行没有任何错误?
    • 如果我评论 whereHas() 它可以工作。使用 whereHas() 它生成这个查询 SELECT * FROM products WHERE (SELECT COUNT(*) FROM products_variations WHERE products_variations.deleted_at IS NULL AND products_variations.product_id = products.@987654335 @ AND products_variations.deleted_at IS NULL GROUP BY id HAVING SUM(price_base + price_engraving) >= '10') >= 1 AND id IN ('346', '1', '1201', '1201')
    • 我感觉groupBy 搞砸了。你能试着评论一下吗?
    • 这就是我想建议的! :)
    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多