eloquent - Laravel Calculate AVG of a related column -


i trying calculate average ratings of related column. relationship (1->many) in between merchant & clientfeedbackreviews models.

merchant model

[merchant model] ::::::::::::::::::::::::::::  public function clientfeedbacks() {     return $this->hasmany('app\clientfeedbackreviews'); //, 'merchant_id', 'id'); 

clientfeedbackreviews

 :::::::::::::::::::::::::::::::::::::::   class clientfeedbackreviews extends model {  protected $table = 'client_feedback_merchants'; public function formerchant() {     return $this->belongsto('app\merchant', 'merchant_id', 'id'); }  public function byclient() {     return $this->belongsto('app\clients', 'client_id', 'id'); } ::::::::::::::::::::::::::::::::::::: 

i need average ratings of merchant (as part of query calculating results-the merchants according nearby distances, given location or using search string depending upon request data)

i have tried found on internet couldn't able 'average_ratings' key-value in results.

here 1 many solutions have tried , pasting here example

 /////// query continued //////////   $getmerchantquery = $getmerchantquery->with(['clientfeedbacks' => function($query) {              $query->select(db::raw('avg( stars) average_rating'));         }]);   /////// query continued ////////// 

and getting - empty array client_feedbacks whereas want average_rating right there.

  { "id": 1, "user_id": 2, "company_name": "best salon", "primary_contact": "111111111", "company_st_address": "office # 62", "company_location": "abc", "company_city": null, "company_state": null, "company_country": null, "company_zip": null, "company_lat": "27.9506", "company_long": "82.4572", "logo_image": "449cbdf0-12ba-11e7-bc65-b7fa1731e4d5.jpeg", "is_fav_by_client_count": 3, "client_feedbacks_count": 1, "user_details": {   "id": 2,   "email": "client@lir.com" }, "client_feedbacks": [] 

}

what options have calculate avg of related table ?

============ edit

however returns results not sure how average in client_feedbacks key.

      $getmerchantquery = $getmerchantquery->with('clientfeedbacks'); 

as

{ "id": 1, "user_id": 2, "company_name": "best salon", "primary_contact": "111111111", "company_st_address": "office # 62", "company_location": "abc", "company_city": null, "company_state": null, "company_country": null, "company_zip": null, "company_lat": "27.9506", "company_long": "82.4572", "logo_image": "449cbdf0-12ba-11e7-bc65-b7fa1731e4d5.jpeg", "is_fav_by_client_count": 3, "client_feedbacks_count": 1, "user_details": {   "id": 2,   "email": "client@lbb.com" }, "client_feedbacks": [   {     "id": 1,     "client_id": 1,     "merchant_id": 1,     "stars": 4,     "review_notes": "good client",     "created_at": null,     "updated_at": null   } ] 

}

ok. did job following blog :

    public function avgfeedback() {      return $this->hasmany('app\clientfeedbackreviews', 'merchant_id', 'id')                     ->selectraw('merchant_id,avg(client_feedback_merchants.stars) average_rating')                     ->groupby('merchant_id'); }  public function getavgfeedbackattribute() {     // if relation not loaded already, let's first     if ($this->relationloaded('commentscount'))         $this->load('avgfeedback');      $related = $this->getrelation('avgfeedback');      // return count directly     return ($related) ? (int) $related->average_rating : 0; } 

and have used follows:

    //////// query continued /////////      $getmerchantquery = $getmerchantquery->with('avgfeedback');      ///////  query continued ////////// 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -