Which column is a foreign key on the AggregateStatistic model?
May 31, 2018
7
Level 1
Sum over multiple column in hasMany relation table
Hi all,
I have a Number Model with hasMany AggregateStatistic relation. AggregateStatistic table has three column ('date','total_bids', 'total_calls');
I'm trying to get all rows from Number model and summing total_bids and total_calls from AggregateStatistic table in Eloquent.
Something like :
App\Number::with('aggregate_statistics')->sum('total_bids','total_calls');
If anyone has an idea , much appreciate.
Level 2
Than you can do something like that:
Number::leftJoin(DB::raw('(SELECT number_id, SUM(total_bids) as binds_sum, SUM(total_calls) as calls_sum FROM AggregateStatisticTableName GROUP BY number_id) AS statistics'), function ($join) {
$join->on('statistics.number_id', '=', 'numbers.id');
})->get();
It does not use your relationship but make just one query
1 like
Please or to participate in this conversation.