How to get user all posts total like?
abrardev99 opened this issue · 4 comments
abrardev99 commented
If user has 5 posts and each post has 5 likes, so there should be all posts total likes = 25.
I think it should like this
$user()->totalLikes()->count()
abrardev99 commented
overtrue commented
use Overtrue\LaravelLike\Like;
$userLikedPosts = $user->likes()->withType(Post::class)->pluck('likeable_id');
$totalLikesOfUserLikedPosts = Like::whereIn('likeable_id', $userLikedPosts)->count();
abrardev99 commented
This is not working as expected. It gives total post likes that give user to posts.
In my case, total likes that users receive in his all posts.
currently, I'm doing it in this way but I think there should be a good way.
$userTotalLikes = 0;
$posts = Post::with('likes')->where('user_id' , Auth::id())->get();
foreach ($posts as $post){
$userTotalLikes = $userTotalLikes + $post->likers()->count();
}
overtrue commented
use Overtrue\LaravelLike\Like;
$userPosts = $user->posts()->pluck('id');
$totalLikesOfUserLikedPosts = Like::whereIn('likeable_id', $userPosts)->count();