Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

anyone ??

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

yeas but i'm groupin it by users.id? or i must group every left join query ?

Last updated 1 year ago.
0

No one knows ?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

that wasn't question and learn english.

Last updated 1 year ago.
0

JoolsMcFly good answer

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ditmens ditmens Joined 23 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.