Используя эту статью, я смог собрать именно то, что мне нужно, вот так ...
Модели веб-сайтов, кампаний и групп:
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;
}
Контроллер:
$websites = Website::select('name', 'id')->
with(
array(
'validClickSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'facebookSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'googleSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'yahooSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'campaigns' => function( $query ) {
$query->where( 'user_id', '=', 2 )->with(
array(
'validClickSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'facebookSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'googleSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'yahooSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'traffic_source',
'groups' => function( $query ) {
$query->with(
array(
'validClickSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'facebookSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'googleSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'yahooSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'keywords' => function( $query ) {
$query->with(
array(
'validClickSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
},
'sourceSums' => function( $query ) {
$query->where( 'user_id', '=', 2 );
}
)
);
}
)
);
}
)
);
}
)
)->get();