Laravel 计算相关列的平均值

Laravel Calculate AVG of a related column

我正在尝试计算相关栏目的平均评分。关系 (1->many) 介于 Merchant 和 ClientFeedbackReviews 模型之间。

商家模型

[Merchant Model]
::::::::::::::::::::::::::::

public function clientFeedbacks() {
    return $this->hasMany('App\ClientFeedbackReviews'); //, 'merchant_id', 'id');

客户反馈评论

 ::::::::::::::::::::::::::::::::::::::: 

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 //////////

这就是我总是得到的 - 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": "client@lir.com"
},
"client_feedbacks": []

}

我们有哪些选项可以计算相关 table 的平均值?

============ 编辑

然而这个 returns 结果却不确定如何在 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": "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
  }
]

}

好的。所以这完成了这个 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 //////////