I am trying to select from a table, but I only want to select things that have an existing relationship.
For example, if I have Users and Comments, and Users haveMany Comments, I want to do something like:
User::hasComments()->paginate(20);
So, I only want to select Users that have at least 1 Comment, and paginate the result of that query. Is there any way to do this?
According to Laravel's Eloquent documentation for querying relations (look for the "Querying Relationship Existence" subheading), this should work:
User::has('comments')->paginate(20);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With