Hello, I am pretty new to Laravel, and can't find a solution to my problem.
I've made a SQL query,
$likes = DB::select("
SELECT count(likes.post_id) AS AllCount, posts.id, posts.image
FROM posts, likes
WHERE likes.post_id = posts.id
GROUP BY likes.post_id, posts.id, posts.image
ORDER BY AllCount DESC
");
It works like charm, but I ran in to a problem of pagination, since I can't just add ->paginate(3); at the very end, and the solution is to use Eloquent query builder. I was trying to convert this code, but I was just not able to. My try is:
$likes = DB::table('posts', 'likes')
->select(DB::raw('count(likes.post_id) as AllCount, posts.id, posts.image'))
->from('posts', 'likes')
->whereRaw('likes.post_id = posts.id')
->groupBy('likes.post_id', 'posts.id', 'posts.image')
->orderBy('AllCount', 'DESC')
->paginate(3);
but all it shows for me, is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes.post_id' in 'where clause' (SQL: select count(*) as aggregate from `posts` where likes.post_id = posts.id group by `likes`.`post_id`, `posts`.`id`, `posts`.`image`)