From 5e9b993447495dc1bb362a0e07a39b27408dc7e3 Mon Sep 17 00:00:00 2001 From: Alexander Wright Date: Mon, 27 Jul 2015 09:47:28 +0100 Subject: [PATCH] Updated paginator with comments from https://github.com/fadion/Bouncy/issues/3 --- composer.json | 2 +- src/Fadion/Bouncy/ElasticCollection.php | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 7cea9b5..e1a9ce9 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "alexanderwright/bouncy", + "name": "fadion/bouncy", "description": "Map Elasticsearch results to Eloquent models", "license": "MIT", "keywords": ["laravel", "elasticsearch", "eloquent"], diff --git a/src/Fadion/Bouncy/ElasticCollection.php b/src/Fadion/Bouncy/ElasticCollection.php index eb7d2b5..ea9fe00 100644 --- a/src/Fadion/Bouncy/ElasticCollection.php +++ b/src/Fadion/Bouncy/ElasticCollection.php @@ -2,6 +2,8 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Pagination\Paginator; +use Illuminate\Pagination\LengthAwarePaginator; + class ElasticCollection extends Collection { @@ -28,12 +30,12 @@ public function __construct($response, $instance) */ public function paginate($perPage = 15) { - $paginator = new Paginator($this->items, $perPage); - - $start = ($paginator->currentPage() - 1) * $perPage; - $sliced = array_slice($this->items, $start, $perPage); + $page = Paginator::resolveCurrentPage() ?: 1; + $path = '/' . \Request::path(); + $sliced = array_slice($this->items, ($page - 1) * $perPage, $perPage); + $total = count($this->items); - return new Paginator($sliced, $perPage); + return new LengthAwarePaginator($sliced, $total, $perPage, $page, compact('path')); } /**