Laravel计算相关列的AVG

时间:2017-04-14 14:01:07

标签: laravel eloquent laravel-5.4 laravel-eloquent laravel-query-builder

我正在尝试计算相关列的平均评分。这种关系(1->多)介于商人与商人之间。 ClientFeedbackReviews模型。

商家模式

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

这就是我总是得到的 - 为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": []

}

我们有什么选择来计算相关表格的平均值?

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

但是这会返回结果但不确定如何在client_feedbacks键中获取AVERAGE。

      $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
  }
]

}

1 个答案:

答案 0 :(得分: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 //////////