Laravel:Profile很多其他型号

时间:2017-04-29 10:07:40

标签: laravel-5.4

合作伙伴, 协议, 协作

合作伙伴有很多协议 协议有很多合作

协作模型有三个字段是志愿者,grant_amount,小时和时间戳

我需要列出所有合作伙伴和志愿者总数以及总拨款金额

你能帮助我如何获得输出吗? 合作伙伴控制器

$partners = Partner::with(['currentYearDegree',
'agreements' => function($query){
 $query->select('id','partner_id','title')
 ->with(['collaborations']);

}])

foreach($partners as $partners )
{
 $partner->grant_amount = $partner->agreements->collaborations->sum('grant_amount')
}

它显示错误消息“调用未定义的方法协作”

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

foreach($partners as $partner )
    {
        $partner->grant_amount = 0;
        $partner->volunteers = 0;
        $partner->hours = 0;
        $partner->students = 0;
        foreach($partner->agreements as $agreement )
        {
            $partner->grant_amount += $agreement->collaborations->sum('grant_amount');
            $partner->volunteers += $agreement->collaborations->sum('volunteers');
            $partner->hours += $agreement->collaborations->sum('hours');
            $partner->students += $agreement->collaborations->sum('students');

        }

    }