Laravel Eloquent Eager Load with Sums

时间:2018-05-24 20:01:24

标签: laravel eloquent eager-loading

我试图找出急于加载大量数据的最佳方法,以便我的项目可以快速轻松地加载。如下所示,我们的对象之间存在很多关系,每个级别都需要多次计算和显示的总和。我想知道如何快速加载和显示这些数据。

关系:

Website has campaigns, groups, keywords, valid_click_ads, facebook_ads, google_ads, yahoo_ads
Campaign has groups, keywords, valid_click_ads, facebook_ads, google_ads, yahoo_ads
Group has keywords, valid_click_ads, facebook_ads, google_ads, yahoo_ads
Keywords has valid_click_ads

这是我需要的数据。我希望基本上每个关系级别(WHERE created_at =今天的所有数据)

获取此数据
Object -> name
          sum(valid_click_ads->revenue)
          sum(facebook_ads->spend)
          sum(yahoo_ads->spend)
          sum(google_ads->spend)
          sum(valid_click_ads->tq WHERE tq != -1)
          count(tq)
          sum(CASE(tq = -1, 0, 1))
          sum(facebook_ads->impressions)
          sum(yahoo_ads->impressions)
          sum(google_ads->impressions)
          sum(facebook_ads->clicks)
          sum(yahoo_ads->clicks)
          sum(google_ads->clicks)
          sum(valid_click_ads->clicks)

如果有必要,我很乐意提供更多信息。

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您可以使用以下内容:

Website::withCount(['valid_click_ads' => function($query) {
    $query->select(DB::raw('sum(revenue)'))
        ->where('created_at', ...);
}])->get();

答案 1 :(得分:0)

使用this文章,我能够将我所需要的完全拼凑在一起......

网站,广告系列和群组模型:

Open @Hostels
Fetch next from @Hostels into @HostelId
While @@FETCH_STATUS = 0
Begin 
    RAISERROR('%d', 0,0, @HostelId);
    Fetch next from @Hostels into @HostelId
End
Close @Hostels


关键字型号:

public function getValidClickSumsAttribute() {
    if ( ! $this->relationLoaded('validClickSums')) 
        $this->load('validClickSums');

    $related = $this->getRelation('validClickSums');

    return $related;
}

public function facebookSums() {
    return $this->hasOne('App\Models\FacebookAd')->selectRaw('sum(spend) as spend, sum(clicks) as clicks, website_id')->groupBy('website_id');
}

public function getFacebookSumsAttribute() {
    if ( ! $this->relationLoaded('facebookSums')) 
        $this->load('facebookSums');

    $related = $this->getRelation('facebookSums');

    return $related;
}

public function googleSums() {
    return $this->hasOne('App\Models\GoogleAd')->selectRaw('sum(spend) as spend, sum(clicks) as clicks, website_id')->groupBy('website_id');
}

public function getGoogleSumsAttribute() {
    if ( ! $this->relationLoaded('googleSums')) 
        $this->load('googleSums');

    $related = $this->getRelation('googleSums');

    return $related;
}

public function yahooSums() {
    return $this->hasOne('App\Models\YahooAd')->selectRaw('sum(spend) as spend, sum(clicks) as clicks, website_id')->groupBy('website_id');
}

public function getYahooSumsAttribute() {
    if ( ! $this->relationLoaded('yahooSums')) 
        $this->load('yahooSums');

    $related = $this->getRelation('yahooSums');

    return $related;
}


控制器:

public function validClickSums() {
    return $this->hasOne('App\Models\ValidClickAd')->selectRaw('sum(impressions) as impressions, sum(revenue) as revenue, sum(clicks) as clicks, count(tq) as unscored_tq_count, sum(case when tq >= 0 then 1 else 0 end) as scored_tq_count, sum(case when tq >= 0 then tq else 0 end) as tq, keyword_id')->groupBy('keyword_id');
}

public function getValidClickSumsAttribute() {
    if ( ! $this->relationLoaded('validClickSums')) 
        $this->load('validClickSums');

    $related = $this->getRelation('validClickSums');

    return $related;
}