简体   繁体   English

雄性幼虫急切加载的条件

[英]conditions in eager loading in eloquent laravel

I get my Product be this code. 我得到我的产品是这样的代码。 I use with() method to load variations relation. 我使用with()方法加载变化关系。 I have some filters for it. 我有一些过滤器。 But if any variations does't exist I get Product. 但是,如果不存在任何变化,我会得到产品。

How can I get only this Product where variations exist ? 我如何才能仅获得存在变体的产品?

$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);

To accomplish that you have to do pretty much the same thing again but with whereHas . 要实现这一点,您必须再次做几乎相同的事情,但要使用whereHas Since the closure will be exactly the same we can put it in a variable to avoid duplicate code: 由于闭包将完全相同,因此我们可以将其放入变量中以避免重复代码:

$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);

(By the way, there's no need to return $query at the end of the anonymous function) (顺便说一句,在匿名函数的末尾不需要返回$query

Edit 编辑

As we figured out you need to change the groupBy statements to where 正如我们想通了,您需要更改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
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM