Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Azoruk's avatar

Paginate value from multiple nested relationships

I apologize if this isn't a great question but I'm still getting used to eloquent relationships.

On my website users can be moderators of subchans (sub channels). Subchans have modmail that all moderators of that subchan can see. Sort of like reddit.

So I have a user model, and in this user model I have a relationship to the Moderator model:

public function modOf()
{
    return $this->hasMany(Moderator::class, 'user_id', 'id')->where('activated', 1);
}	

then in the moderator model, I have a relationship that points to the subchan model:

public function subchan()
{
    return $this->belongsTo(\App\Subchan::class, 'subchan_id');
}

then in the subchan model, I have a relationship that returns all the Modmail for that subchannel:

public function modMail()
{
    return $this->hasMany(\App\ModMail::class, 'subchan_id');
}

So now I want to create a page where I can foreach and paginate all the modmail that the Auth user has.

How do I pass only a set of modmail paginated results from the controller into the view?

0 likes
6 replies
jlrdw's avatar

You may need to set up some custom pagination with the lengthaware paginator, see

https://laracasts.com/discuss/channels/laravel/implementation-of-multiple-pagination-on-laravel-7

And the laravel API describes the lengthaware paginator.

Something like:

Acme, Inc       (Link for next company here)

===================================

     Receivable on Feb  5, 2021
     Receivable on Feb  7,  021
     Receivable on Feb 10, 2021
     Receivable on Feb 14, 2021
     Receivable on Feb 25, 2021

(Link for this companies next page of Receivables here)

Azoruk's avatar

I see. Well if it's going to be complicated, should I just get make a query to get the moderator IDs that the user has, then a query to get the subchan IDs those moderator entries have, then a query to get the modmail from those subchans?

jlrdw's avatar

Seems you could just paginate all the modmail that the Auth user has

So top:


Auth user

=============================

paginate here all the modmail

If only the one Auth user. Use where and orderby as needed in the query.

Azoruk's avatar

Ok, but I don't really know how to do that in this instance.

Azoruk's avatar

I decided to do this the normal way. I thought there would be an eloquent relationship way to do it but I guess not. Here is my solution:

	$modOfSubchanIDs = Auth::user()->modOf->pluck('id')->toArray();
	
	$modMails = ModMail::whereIn('subchan_id', $modOfSubchanIDs)->paginate(25);
jlrdw's avatar

Regular paginate should work. Isn't what needs paginated in one table. So you are searching for the foreign key of that Auth user. Or however you set up the foreign key.

For a second get relations out of your mind and look at it like I need to search a certain table for certain data, then paginate that data.

Please or to participate in this conversation.