在laravel模型中计算avg()并将其传递给控制器

时间:2017-05-26 12:33:13

标签: php json laravel laravel-5 eloquent

我想在laravel中使用三个表连接来计算平均值我完全加入了表并得到了它的响应但现在我想从我得到的响应计算平均值和计数然后将其传递给API响应我该怎么做才能实现这个目标?

我已经尝试过查询

$individualScore= StatisticsMaster::with(['ratings',])->where('ratingsFor',$request->profileId)->get();

StatisticsMaster.php

public function ratings() {
    return $this->hasMany(StatisticsChild::class,'statisticsMasterId','statisticsMasterId')->with('score');
}

StatisticsChild.php

public function score() {
    return $this->belongsTo(Skill::class,'skillId','skillId');
}
public function getSkillNameAttribute(){
    return $this->score->skillName;
}

我有数据库结构

tblSkills

->skillId
->skillName
->skillStatus

tblStatisticsMaster

->statisticsMasterId
->ratingsFor
->ratingsBy
->status

tblStatisticsChild

->statisticsChildId
->skillId
->statisticsMasterId
->individualScore
->statisticsStatus

我收到了这个回复

{
  "meta": {
    "code": "200",
    "message": "Skill Fetched Successfully."
  },
  "data": {
    "skills": [
      {
        "statisticsMasterId": 1,
        "ratingsFor": 1,
        "ratingsBy": 6,
        "score": 60.8,
        "ratings": [
          {
            "skillId": 1,
            "individualScore": 86,
            "statisticsStatus": 1,
            "skillName": "Shooting"
          },
          {
            "skillId": 2,
            "individualScore": 70,
            "statisticsStatus": 1,
            "skillName": "Dribbling"
          },
          {
            "skillId": 3,
            "individualScore": 54,
            "statisticsStatus": 1,
            "skillName": "Passing"
          },
          {
            "skillId": 4,
            "individualScore": 44,
            "statisticsStatus": 1,
            "skillName": "Defense"
          },
          {
            "skillId": 5,
            "individualScore": 50,
            "statisticsStatus": 1,
            "skillName": "Good Teammate"
          }
        ]
      },
      {
        "statisticsMasterId": 2,
        "ratingsFor": 1,
        "ratingsBy": 2,
        "score": 52,
        "ratings": [
          {
            "skillId": 1,
            "individualScore": 50,
            "statisticsStatus": 1,
            "skillName": "Shooting"
          },
          {
            "skillId": 2,
            "individualScore": 50,
            "statisticsStatus": 1,
            "skillName": "Dribbling"
          },
          {
            "skillId": 3,
            "individualScore": 60,
            "statisticsStatus": 1,
            "skillName": "Passing"
          },
          {
            "skillId": 4,
            "individualScore": 50,
            "statisticsStatus": 1,
            "skillName": "Defense"
          },
          {
            "skillId": 5,
            "individualScore": 50,
            "statisticsStatus": 1,
            "skillName": "Good Teammate"
          }
        ]
      }
    ]
  }
}

和我想要的回应。

    {
      "meta": {
        "code": "200",
        "message": "Skill Fetched Successfully."
      },
      "data": {
        "skills": [
          {
            "statisticsMasterId": 1,
            "ratingsFor": 1,
            "ratingsBy": 6,
            "score": 56.4,
            "ratings": [
              {
                "skillId": 1,
                "individualScore": 86,
                "statisticsStatus": 1,
                "skillName": "Shooting"
              },
              {
                "skillId": 2,
                "individualScore": 70,
                "statisticsStatus": 1,
                "skillName": "Dribbling"
              },
              {
                "skillId": 3,
                "individualScore": 54,
                "statisticsStatus": 1,
                "skillName": "Passing"
              },
              {
                "skillId": 4,
                "individualScore": 44,
                "statisticsStatus": 1,
                "skillName": "Defense"
              },
              {
                "skillId": 5,
                "individualScore": 50,
                "statisticsStatus": 1,
                "skillName": "Good Teammate"
              }
            ]
          }
       ]
    }
 }

其中所有的个人得分都包含了我所使用的所有评级的平均值。我想在雄辩中做到这一点。我怎么能实现这个目标呢?

提前致谢:)

0 个答案:

没有答案