2

我正在計算相關列的平均評分。關係(1->很多)位於Merchant & ClientFeedbackReviews模型之間。Laravel計算相關列的AVG

淨價模式

[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'); 
} 
::::::::::::::::::::::::::::::::::::: 

我需要得到所有商家(的平均收視率作爲查詢的一部分,在那裏我計算的結果,在商家根據附近的距離,給定的地點或使用搜索字符串,根據請求數據

我已經嘗試了幾乎所有我在互聯網上找到的東西,但無法在結果中獲得'average_ratings'鍵值。

這是一個很多的解決方案,我已經嘗試了和粘貼在這裏作爲一個例子

/////// query continued ////////// 

$getMerchantQuery = $getMerchantQuery->with(['clientFeedbacks' => function($query) { 

      $query->select(DB::raw('AVG(stars) AS average_rating')); 
     }]); 

/////// query continued ////////// 

,這在我所得到ALWAYS - 爲client_feedbacks空數組,而我想要的AVERAGE_RATING就在那裏。

{ 
"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": "[email protected]" 
}, 
"client_feedbacks": [] 

}

哪些選擇呢我們要計算平均相關表的?

============編輯

但是這個返回的結果,但不知道如何獲得client_feedbacks關鍵的平均值。

 $getMerchantQuery = $getMerchantQuery->with('clientFeedbacks'); 

作爲

{ 
"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": "[email protected]" 
}, 
"client_feedbacks": [ 
    { 
    "id": 1, 
    "client_id": 1, 
    "merchant_id": 1, 
    "stars": 4, 
    "review_notes": "good client", 
    "created_at": null, 
    "updated_at": null 
    } 
] 

}

+0

這是近3天,沒有答案了... – Zeshan

回答

0

確定。因此,這做的工作跟隨這blog

public function avgFeedback() { 

    return $this->hasMany('App\ClientFeedbackReviews', 'merchant_id', 'id') 
        ->selectRaw('merchant_id,AVG(client_feedback_merchants.stars) AS average_rating') 
        ->groupBy('merchant_id'); 
} 

public function getavgFeedbackAttribute() { 
    // if relation is not loaded already, let's do it first 
    if ($this->relationLoaded('commentsCount')) 
     $this->load('avgFeedback'); 

    $related = $this->getRelation('avgFeedback'); 

    // then return the count directly 
    return ($related) ? (int) $related->average_rating : 0; 
} 

,我已經如下使用它:

//////// query continued ///////// 

    $getMerchantQuery = $getMerchantQuery->with('avgFeedback'); 

    /////// query continued //////////