Eloqent获取用户帖子" with()" COUNT个评分(1-5)

时间:2017-07-16 17:39:21

标签: laravel eloquent laravel-5.4

我对Laravels Eloquent有疑问。

我有:

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

我需要获取用户创建的所有帖子,并获取第三个表格中的rating_value(1-5)的计数" rating",以便我可以向用户显示多少其他人评定了每个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();
}

所以我确实获得了所有帖子,但问题是,对于其中一个结果,我得到了所有帖子的评级值的计数,而不是我认为我使用&#34查询的特定帖子; $查询"

所以当三个帖子的评分为" + 1"每一个,其中一个显示拥有所有那些" + 1" -ratings,其他人回来是空的。

我不明白为什么?

部分(剥离)回复:

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

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以加载数据:

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

使用map()count()收集方法创建一个具有计数评分的新媒体资源:

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

    return $item;
});

答案 1 :(得分:0)

感谢Alexey Mezenins的回答,我最终解决了这个问题:

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;
    });

}

一定要告诉我它是否有任何问题!