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

zidance's avatar

How to filter result from pivot relationship table?

I have 3 tables users, category_user, categories.

category_user is my pivot table.

I want to get the users with certain category.

I am writing in this way to get the listing

User::with('categories')->orderBy('popularity', 'desc')->paginate(20)

But I am facing an issue when trying to filter the category_id, then it shows column not found. I also tried function method for the relationship but the result not get filtered.

How can I actually filter the result if without using whereHas?

0 likes
3 replies
zidance's avatar

@SilenceBringer Is this the only way to achieve it? Because I am doing some research show that whereHas might causing the performance issue.

Please or to participate in this conversation.