How to calculate the total likes of all posts of a user

Published 4 months ago by Dev0ps

i need to count users all posts like

```

$post = Post::whereUserId(auth::id())->get('id');

$postlikes = PostLike::wherePostId($post)->count('post_id);


it's work in foreach. but I am looking for some more convenient way

jdc1898

Did you try this?

$post = Post::whereUserId(auth::id())->get('id')->count();

Dev0ps

bro, it counts the only post. I wanna count post likes

jdc1898

Sorry didn't see your second example.

Can you post you schema?

oroalej

Try using withCount.

you can read further in here

Please sign in or create an account to participate in this conversation.