Add/fix support for distinct selects when using pagination

This adds/fixes the ability to use `select()->distinct()->paginate()`. Previously, the count would be off because the count query disregarded `distinct()`.

Fixes #3191