2017-07-16 12 views
2

Laravels Eloquentに問題があります。Eloqent fetchユーザーの投稿「with()」評価の数(1-5)

私が持っている:

Users (hasMany(Posts)) 
Posts (hasMany(Ratings)) 
Ratings (belongsTo(Post) 
    - has "rating_value" of 1-5 

私はそのように、ユーザが作成したすべての記事をフェッチし、またrating_valueのカウント(1-5)第三のテーブルに存在する「評価」を取得する必要があります各rating_valueを評価した他のユーザーの数を表示できます。

Eg view of users own posts: 
    Post 1: 30 (other users) rated +1, 50 rated +2, ... etc. up to "rated +5". 
    Post 2: ... 
    ... 

私は経験豊富なEloquentersが問題を見ることを願っていますコード...

private function fetchUserPosts($request) { 
    $this -> userPosts 
     = $this -> user 
     -> posts() 
     -> with([ 
      'ratings' => function ($query) { 
       $query -> selectRaw('post_id, rating_value, count(*) AS count') 
        -> groupBy('rating_value'); 
      } 
     ]) 
     -> get(); 
} 

は、だから私はすべてのポストを得るかが、問題は結果の一つのために私が得るということですその評価値の数、すべての投稿、私は "$クエリ"を使って質問していたと思った特定の投稿ではありませんか?

3つの投稿の評価がそれぞれ「+1」である場合、そのうちの1つが「+1」の評価を表示し、残りの1つが空に戻ります。

なぜわかりませんか?

部分(剥奪)応答:事前に

"userPosts": [ 
    { 
     "id": 2, 
     "user_id": 2, 
     "ratings": [] // SHOULD have "rating_value": 1, "count": 1 
    }, 
    { 
     "id": 3, 
     "user_id": 2, 
     "ratings": [] // SHOULD have "rating_value": 1, "count": 1 
    }, 
    { 
     "id": 4, 
     "user_id": 2, 
     "ratings": [ 
      { 
       "post_id": 4, 
       "rating_value": 2, 
       "count": 1 // This works fine as there is only one post by this user that anyone rated 2, but then it gets messed up... 
      } 
     ] 
    }, 
    { 
     "id": 5, 
     "user_id": 2, 
     "ratings": [ 
      { 
       "post_id": 5, 
       "rating_value": 1, 
       "count": 3 
      } 
     ] 
    }, 

// To clearify the following lines are added manually to show how it would look with a real, larger user base: 

    { 
     "id": 6, 
     "user_id": 2, 
     "ratings": [ 
      { 
       "post_id": 6, 
       "rating_value": 1, 
       "count": 25 
      }, 
      { 
       "post_id": 6, 
       "rating_value": 2, 
       "count": 102 
      }, 
      { 
       "post_id": 6, 
       "rating_value": 3, 
       "count": 509 
      }, 
      { 
       "post_id": 6, 
       "rating_value": 4, 
       "count": 204 
      }, 
      { 
       "post_id": 6, 
       "rating_value": 5, 
       "count": 57 
      } 
     ] 
    } 

ありがとう!

答えて

0

感謝このような問題:

private function fetchUserPosts($request) { 

    $userPosts = $this->user->posts()->with('ratings')->get(); 

    $this -> userPosts = $userPosts -> map(function ($item) { 

     $item -> ratingCounts = new \stdClass(); 

     $item -> ratingCounts -> veryPositive = $item -> ratings -> where('rating_value', 5) -> count(); 
     $item -> ratingCounts -> positive = $item -> ratings -> where('rating_value', 4) -> count(); 
     $item -> ratingCounts -> neutral = $item -> ratings -> where('rating_value', 3) -> count(); 
     $item -> ratingCounts -> negative = $item -> ratings -> where('rating_value', 2) -> count(); 
     $item -> ratingCounts -> veryNegative = $item -> ratings -> where('rating_value', 1) -> count(); 

     // To not return raw rating-rows that includes rating users id etc... 
     unset($item -> ratings); 

     return $item; 
    }); 

} 

それに問題がある場合は、是非教えてください!

0

あなたがデータをロードできます。

$this->userPosts = $this->user->posts()->with('ratings')->get(); 

を、カウント評価と新しいプロパティを作成するmap()count()収集方法を使用します。私は解決してしまったアレクセイMezeninsの答えに

$this->userPosts = $this->userPosts->map(function ($item) { 
    for ($i = 1; $ <= 5; $i++) { 
     $item->rating{$i} = $item->ratings->where('rating_value', $i)->count(); 
    } 

    return $item; 
}); 
+0

ありがとう、これは自分のような初心者のための素晴らしい情報です!しかし、評価の合計は必要ありません.1/2/3/4/5の評価は単なる評価のタイプであり、何も意味するものではありません。私が必要とするのは、これらのどれがどれくらいあるかのカウントです。 – Fredrik

+0

@Fredrik私の答えの**アップデート**セクションをお読みください。それはあなたが探しているものですか? –

+0

私たちはそこに着いていますが、rating_valueの種類ごとに数が必要です。したがって、評価値1にはx評価があります)/ 2にはy評価など/ 3/4/5があります。どのようにデータを提供したいかの例について、私が提供した回答の一番下の例を見てください。 – Fredrik

関連する問題