Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 LengthAwarePaginator

Tags:

laravel

My brain suddenly crashed on this one. Anyone care to help me is highly appreciated.

This is LengthAwarepaginator in laravel 5.4

Here is the code.

$collection = [];

            foreach ($maincategories->merchantCategory as $merchantCat) {

               foreach ($merchantCat->merchantSubcategory as $merchantSub) {

                   foreach($merchantSub->products as $products){

                        $collection[] = $products;
                   }
               }
            }

            $paginate = new LengthAwarePaginator($collection, count($collection), 10, 1, ['path'=>url('api/products')]);

            dd($paginate);

It displays perfectly but the problem is the items is 100. That's all my items and I specify it correctly. I need to display only 10.

Base on LengthAwarePaginator constructor. Here is the reference.

public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])

Here is the screen shot.

enter image description here

Where did I go wrong? TY

like image 863
Rbex Avatar asked Apr 25 '17 03:04

Rbex


2 Answers

When manually creating a paginator, you have to slice the result set yourself. The first parameter to the paginator should be the desired page of results, not the entire result set.

From the pagination documentation:

When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the array_slice PHP function.

I would suggest using a Collection to help out with this a little:

// ...

$collection = collect($collection);

$page = 1;
$perPage = 10;

$paginate = new LengthAwarePaginator(
    $collection->forPage($page, $perPage),
    $collection->count(),
    $perPage,
    $page,
    ['path' => url('api/products')]
);
like image 126
patricus Avatar answered Nov 15 '22 23:11

patricus


For the heavy select and to avoid any multiple select to calculate the total from the table we unable to use model paginate

use Illuminate\Pagination\LengthAwarePaginator;

in your controller function

if(!isset($input["total"])){ //initial request
         $total = //Find the total only one time.
         $request->request->add(['total' => $total]); //Add it in the request so that we can print it in blade 
}else
         $total = $input["total"];  //After initial request

$currentPage = LengthAwarePaginator::resolveCurrentPage(); //page variable from GET or POST
$perPage = 30; //Page Length 
$offset = ($currentPage - 1) * $perPage; //find the offset to pass in query
$specificRecords = /*Collect specific page records in array 
if mysql then Select * from table limit $perPage offset $offset 
if ms sql then OFFSET {$offset} ROWS FETCH NEXT {$perPage} ROWS ONLY */
$records = new LengthAwarePaginator($specificRecords,$total,$perPage,Null,[ "path" => "/pagepath" ]);

in blade:

<center>{{$records->appends(Request::except([ 'page','_token' ]))->links()}}</center>

Check Page and total variable in page tags ensure you added page in except list :)

like image 1
Ashik Ali Avatar answered Nov 16 '22 00:11

Ashik Ali