How to use best foreach and use paginate after query