Fix pagination when using distinct() in Query or Eloquent

Pagination works correcty when using groupBy(), but not when distinct() is used. I think this patch fixes this issue, but please have a good look at it as I don't know the ins and outs of L4.