When you count something you need to group by a column.
In your case, SQL would look like:
select user_id, username, count(*) as votes
from users u
join user_votes uv on u.id=uv.user_id
group by u.id; // this is really important
You can also get away with one query:
select user_id, username, count(*) as votes
from users u
join user_votes uv on u.id=uv.user_id
join profiles p on p.user_id=u.id
group by u.id;
I'll let you figure out how to port above code to Eloquent.
yeas but i'm groupin it by users.id? or i must group every left join query ?
You have COUNT inside your select statement. You must not call count on that query. I you want the number of all users then just take it.
$user_count = User::count();
see what your queries result in, and learn SQL.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community