Muhammad Atallah
Muhammad Atallah

Reputation: 976

Manually Creating A Paginator in Laravel 5.1

I try to create a pagination instance manually like the following

use Illuminate\Pagination\LengthAwarePaginator as Paginator;

 public function createPaginate()
 {

  $page =1;  // Get the current page or default to 1
  $perPage = 2;
  //this is my array 
  $items = $this->getCurrentServices($this->getServices(new MaintenanceService, 'maintenance_service')->get());

  $offset = ($page * $perPage) - $perPage;

  $maintenanceServices = new Paginator (
                         array_slice($items, $offset,  $perPage, true),
                         count($items),$perPage,Paginator::resolveCurrentPage(), 
                         array('path' =>  Paginator::resolveCurrentPath())
                        );

  return view('my-view', compact('maintenanceServices'));

The above code working well in my-view but the data is not change when switch between the pages

Also i tried to change my code with Request to check url

 $maintenanceServices =new Paginator (
                       array_slice($items, $offset, $perPage, true),
                       count($items), $perPage, $page, 
                       ['path' => $request->url(), 'query' => $request->query()]);

and finally i checked $items using dd($items) the result was

array:7 [▼
0 => MaintenanceService {#245 ▶}
1 => MaintenanceService {#246 ▶}
2 => MaintenanceService {#247 ▶}
3 => MaintenanceService {#248 ▶}
4 => MaintenanceService {#249 ▶}
5 => MaintenanceService {#250 ▶}
6 => MaintenanceService {#251 ▶}
     ]

Any Suggestions ?

Upvotes: 2

Views: 1322

Answers (2)

user5672728
user5672728

Reputation:

In laravel 5.1 you can do the follwing

$pageStart = request()->get('page', 1);
$offset = ($pageStart * $perPage) - $perPage;

Upvotes: 2

Muhammad Atallah
Muhammad Atallah

Reputation: 976

I found the solution here i'm was not passing the current page.

$pageStart = \Request::get('page', 1);
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage; 

Upvotes: 0

Related Questions