@zidance check it here https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
User::whereHas('categories', fn ($query) => $query->where('id', $categoryId))
->with('categories')
->orderBy('popularity', 'desc')
->paginate(20)