0
Currently I have a query that takes the posts and searches through Join some user data
$posts = \App\Post::join('users', 'users.id', '=', 'posts.author_id')
->select('posts.id', 'posts.title', 'posts.excerpt', 'posts.slug', 'posts.created_at', 'posts.image', 'users.avatar', 'users.name', 'users.username')
->orderBy('posts.created_at', 'desc')
->paginate(14);
I recently implemented a simple like system, facebook style and would like to add in the same query the sum of Likes according to the post
I tried to add like in the same query as follows
$posts = \App\Post::join('users', 'users.id', '=', 'posts.author_id')
->join('likes', 'posts.id', '=', 'likes.post_id')
->select('posts.id', 'posts.title', 'posts.excerpt', 'posts.slug', 'posts.created_at', 'posts.image', 'users.avatar', 'users.name', 'users.username', DB::raw('count(likes.post_id) as likeCount'))
->groupBy('likes.id')
->orderBy('posts.created_at', 'desc')
->paginate(14);
How could I resolve the query ? Join solves or better another method.