Support the ongoing development of Laravel.io →
Database Eloquent

I am trying to select from a table, but I only want to select things that have an existing relationship.

So, like, if I have Users and Comments. Users haveMany Comments.

I want to do something like:

User::hasComments()->paginate(20);

So, I only want to paginate users that have at least 1 comment. Is there any way to do this?

Thanks!

Last updated 2 years ago.
0

You're close! ;)

User::has('comments')->paginate();

0

Yup, figured that out and forgot to report back haha. Thanks!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ryuske ryuske Joined 20 May 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.

© 2025 Laravel.io - All rights reserved.