1

I have post Model which has multiple reactions (Like, dislike etc)

I want to count those reaction by their attr_id. I tried below code and it gives total number of reactions on post.

public function PostReaction() {
        return $this->hasMany('App\PostReaction');
    }

public function getAttributesCountAttribute() {
        return $this->PostReaction()->count();
}

and if I do

return $this->PostReaction()->groupBy('attr_id')->count();

It returns only first reaction count instead of all. It should give array of count with all reactions

Sainish Momin
  • 95
  • 1
  • 12
  • count() doesn't return an array, so it shouldn't give an array of the counts. – Devon Bessemer Oct 06 '17 at 12:50
  • Possible duplicate of [Laravel Eloquent groupBy() AND also return count of each group](https://stackoverflow.com/questions/18533080/laravel-eloquent-groupby-and-also-return-count-of-each-group) – Devon Bessemer Oct 06 '17 at 12:51
  • The query builder and ORM methods are the same... did you even try it? – Devon Bessemer Oct 06 '17 at 14:14
  • I mean to say eloquently. – Sainish Momin Oct 06 '17 at 14:17
  • Eloquently? Eloquent is the ORM and Eloquent extends the query builder so the methods are the same. – Devon Bessemer Oct 06 '17 at 14:21
  • I already have tried below code return DB::table('post_reaction_relation') ->select('attr_id', DB::raw('count(*) as total,attr_id')) ->groupBy('attr_id') ->get(); it will not use model relationship it will give you all results instead of count only related to this post – Sainish Momin Oct 06 '17 at 14:26
  • I figured it out. And yes I may not know everything since new to Laravel. And it would be more cool if you could have answered in a polite way. Never think you know everything – Sainish Momin Oct 06 '17 at 14:31

3 Answers3

0

I think that you have this error because $this->PostReaction returns the reactions of all posts.

Try to call the function outside the model by making a repository class where you'll have the method getAttributesCountAttribute and pass to it the $post on which you want to count the reactions. Something like:

public function getAttributesCountAttribute($post) {
        return $post->PostReaction->count();
}

Same thing if there is multiple posts, try to loop for each post and get its reactions not all at once.

Belahcel Yanis
  • 193
  • 2
  • 9
  • Why downvote? Can you explain what is not clear to help move his issue forward? – Belahcel Yanis Oct 06 '17 at 14:24
  • @B_Yanis I don't see how this is an answer. How does this solve the problem at hand? It just adds a level of abstraction but will return the same result as count() returns an integer, not an array. – Devon Bessemer Oct 06 '17 at 14:29
0

I solved it by using below code

return $this->PostReaction()
                 ->select('attr_id', DB::raw('count(*) as total,attr_id'))
                 ->groupBy('attr_id')
                 ->get();
Sainish Momin
  • 95
  • 1
  • 12
-1

Have you tried $this->PostReaction->count(); without the () in PostReaction? It is a dynamic property provided automatically by Eloquent.

Make sure to define a relationship in the PostReaction Model too the PostReaction belongsTo one Post.

If the post has many reactions I think it should be clearer to call the method reactions and count the reactions like that : $post->reactions->count(); If you want to get only the 'likes' you can chain the conditions like that : $post->reactions->where('reaction', 'like')->count();

For more details you can check the OneToMany relashionship in the documentation

Belahcel Yanis
  • 193
  • 2
  • 9
  • I tried without () in count but it throws an error.Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$count – Sainish Momin Oct 06 '17 at 13:57