2

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 ?

2 Answers 2

2

In laravel 5.1 you can do the follwing

$pageStart = request()->get('page', 1);
$offset = ($pageStart * $perPage) - $perPage;
Sign up to request clarification or add additional context in comments.

Comments

0

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; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.