【问题标题】:Get Count Of Nested Entities using Laravel Eloquent使用 Laravel Eloquent 获取嵌套实体的数量
【发布时间】:2015-12-02 12:40:45
【问题描述】:

我有 3 个数据库表客户、优惠券和类别

clients table

身份证, 姓名, 网站, 描述, 商标, 蛞蝓

categories table

身份证, 姓名, 蛞蝓

coupons table

身份证, 客户 ID, 类别 ID, 类型, 优惠券, 标题, 描述, 关联, 意见, 蛞蝓, 到期

关系是 1)许多优惠券属于客户(多对一关系) 2)许多优惠券属于类别(多对一关系)

我使用的是 laravel 5.1。

如何通过客户详细信息、客户拥有的优惠券数量以及单个客户拥有的总类别数来获取客户的唯一数量。

简化:我需要获取客户详细信息并显示特定客户的 xxx 个类别中有 xxx 个优惠券可用。

到目前为止,我可以获得唯一的客户详细信息和优惠券数量。

 public function getAvailableClientsWithItemCountList($page = 1)
{
    return Client::join('coupons', 'clients.id', '=', 'coupons.client_id')
        ->join('categories', 'coupons.category_id', '=', 'categories.id')
        ->where('coupons.expiry', '>', Carbon::today())
        ->groupBy('clients.id')
        ->skip(STORES_PER_REQUEST*($page-1))
        ->take(STORES_PER_REQUEST)
        ->get(['clients.id', 'clients.name', 'clients.slug', 'clients.logo', DB::raw('count(clients.id) as dealsCount'), DB::raw('count(categories.id) as categoriesCount')]);
}

STORES_PER_REQUEST = 9(常量)用于分页。 提前致谢。

【问题讨论】:

    标签: mysql eloquent laravel-5.1


    【解决方案1】:

    如果您建立了关系,您可以执行以下操作:

     /**
     * Mock relationship for eager loading coupon count
     *
     * @return mixed
     */
    public function couponCount()
    {
        return $this->hasOne(Coupon::class)
            ->selectRaw('client_id, count(*) as aggregate')
            ->groupBy('client_id');
    
    }
    
    public function getCouponCountAttribute()
    {
        // if relation is not loaded already, let's do it first
        if (!$this->relationLoaded('couponCount')) {
            $this->load('couponCount');
        }
    
        $related = $this->getRelation('couponCount');
    
        // then return the count directly
        return ($related) ? (int) $related->aggregate : 0;
    }
    

    以上内容可以在您的Client 模型中使用,然后您可以为您的Category 模型更改couponCount 关系方法(如果您愿意的话)。

    然后为您的Category 计数添加以下内容:

     /**
     * Mock relationship for eager loading category count
     *
     * @return mixed
     */
    public function categoryCount()
    {
        return $this->hasOne(Coupon::class)
            ->selectRaw('category_id, count(*) as aggregate')
            ->groupBy('client_id, category_id');
    
    }
    
    public function getCategoryCountAttribute()
    {
        // if relation is not loaded already, let's do it first
        if (!$this->relationLoaded('categoryCount')) {
            $this->load('categoryCount');
        }
    
        $related = $this->getRelation('categoryCount');
    
        // then return the count directly
        return ($related) ? (int) $related->aggregate : 0;
    }
    

    然后,您可以在 Coupon 模型中添加查询范围,以获取尚未过期的优惠券,例如:

    public function scopeActive($query)
    {
        $query->where('expiry', '>', Carbon::today());
    }
    

    如果您只想获得尚未过期的优惠券的数量,您可以添加您可以将其直接添加到关系中,例如 groupBy('client)id')->active()

    现在您应该可以像这样急切加载关系了:

     $clients = Client::with('couponCount', 'clientCount')
        ->skip(STORES_PER_REQUEST * ($page - 1))
        ->take(STORES_PER_REQUEST)
        ->get();
    

    或者您可以将查询范围附加到急切负载,即

     $clients = Client::with(['couponCount' => function ($q) {$q->active()}, 'clientCount' => function ($q) {$q->active()}]) ...
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢.. 在浪费了一整天的时间寻找解决方案后,我想通了。
    【解决方案2】:

    好的,我自己了解了优惠券类型和可用类别等附加信息。 我所做的关键是在计数中添加了案例并删除了类别表的连接。

    最终代码如下所示

    return App\Client::join('coupons', 'clients.id', '=', 'coupons.client_id')
        ->where('coupons.expiry', '>', \Carbon\Carbon::today())
        ->orderBy('clients.position', 'desc')
        ->groupBy('clients.id')
        ->skip(STORES_PER_REQUEST*(1-1))
        ->take(STORES_PER_REQUEST)
        ->get(['clients.id', 'clients.name', 'clients.slug', 'clients.logo', DB::raw('count(clients.id) as total'), DB::raw('count(CASE WHEN coupons.type=\'Coupon\' THEN 1 ELSE NULL END) as couponsCount'), DB::raw('count(CASE WHEN coupons.type=\'Deals\' THEN 1 ELSE NULL END) as dealsCount'), DB::raw('count(Distinct category_id) as categoriesCount')]);
    

    结果是

    [{
    "id": "8",
    "name": "Archies Online",
    "slug": "archies-online",
    "logo": "Archiesonline.jpg",
    "total": "22",
    "couponsCount": "20",
    "dealsCount": "2",
    "categoriesCount": "9"
    }, {
    "id": "5",
    "name": "Shop Clues",
    "slug": "shop-clues",
    "logo": "Shopclues.jpg",
    "total": "24",
    "couponsCount": "24",
    "dealsCount": "0",
    "categoriesCount": "9"
    }, {
    "id": "6",
    "name": "Lens Kart",
    "slug": "lens-kart",
    "logo": "Lenskart.jpg",
    "total": "25",
    "couponsCount": "25",
    "dealsCount": "0",
    "categoriesCount": "8"
    }, {
    "id": "7",
    "name": "Amazer",
    "slug": "amazer",
    "logo": "Amzer.jpg",
    "total": "21",
    "couponsCount": "21",
    "dealsCount": "0",
    "categoriesCount": "8"
    }, {
    "id": "1",
    "name": "Flipkart",
    "slug": "flipkart",
    "logo": "Flipkart.jpg",
    "total": "17",
    "couponsCount": "17",
    "dealsCount": "0",
    "categoriesCount": "9"
    }, {
    "id": "2",
    "name": "Make My Trip",
    "slug": "make-my-trip",
    "logo": "Makemytrip.jpg",
    "total": "11",
    "couponsCount": "11",
    "dealsCount": "0",
    "categoriesCount": "8"
    }]
    

    这已经成功了 :);

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2019-07-25
      • 2022-08-09
      • 2021-08-11
      • 2015-06-27
      相关资源
      最近更新 更多