vendredi 26 mai 2017

Calculate avg() within laravel model and pass it to controller

I want to calculate avg in laravel using three table joins I've joined the tables perfectly and got the response from it but now I want to calculate the avg as well as count from the response I have got and then pass it to API response what should I do to achieve this?

I have got this response

{
  "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"
          }
        ]
      }
    ]
  }
}

and the response I want.

    {
      "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"
              }
            ]
          }
       ]
    }
 }

where all the individual score contains the avg of all the ratings I'm using eloquent I want to do this in eloquent itself. how can I achieve this?

Thanks in advance :)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2r4V3ei
via IFTTT

Aucun commentaire:

Enregistrer un commentaire