0

I have an array of results (more than 25 result) fetched from the database. I am trying to implement Laravel Pagination in my application.

What I want is paginate the results fetched.

What I get is pagination of the same results.

The code that I have used so far:

$result = array_slice($fetchAllProducts, 0, 12);

$paginateResults = new Illuminate\Pagination\LengthAwarePaginator($result, count($fetchAllProducts), 12);

I do get the pagination, but when working inspecting, on page 2, on page 3, and so on.. I get the result that is visible on page 1 of the pagination on all the subsequent pages of the pagination.

Kindly help me out with this. Any help is highly appreciated.

EDIT 1: The code

<?php
$productsInParent = $category->products;
$productsInParentCategory = new Illuminate\Support\Collection;
$allProducts = $allProductsFromChild = [];

foreach($productsInParent as $prodInParent) {
    $allProducts[] = $prodInParent;
}

foreach($category->childs as $child) {
    foreach($child->products as $productsInChildren) {
        $allProductsFromChild[] = $productsInChildren;
    }
}

$fetchAllProducts = array_merge($allProducts, $allProductsFromChild);

$result = array_slice($fetchAllProducts, 0, 12);

$paginateResults = new Illuminate\Pagination\LengthAwarePaginator($result, count($fetchAllProducts), 12);
?>

P.S.: I am trying to learn on my own how to integrate custom pagination in Laravel 5.1

Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71

2 Answers2

0

The paginator render will set in the URL the page parameter which should be used to determine the offset for the slice:

$result = array_slice($fetchAllProducts, Request::get('page',1)-1, 12); 

However I'd advise to change the query to get all results in one query and use the QueryBuilder paginator something like:

$categories = $catgory->childs->pluck('id')->all();
$categegories[] = $category->id;

$paginateResults =Product::whereIn('category_id', $categories)->paginate(12)
iipavlov
  • 1
  • 2
  • I tried that as well.. It didn't even show the pagination.. Instead it showed all the results without pagination.... – Saiyan Prince Jun 04 '16 at 14:57
  • sorry, wrote it without getting enough deep. You have manually to set the page when slicing: `$result = array_slice($fetchAllProducts, Request::get('page',1)-1, 12); $paginateResults = new Illuminate\Pagination\LengthAwarePaginator($result, count($fetchAllProducts), 12);` – iipavlov Jun 04 '16 at 15:17
0
<?php

namespace App\Http\Controllers;

use Illuminate\Pagination\LengthAwarePaginator as Paginator;
// use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
use App\Product;
class MyController extends Controller
{
    public function index(Request $request){
        $items = Product::all();

        $filter_products = []; // Manual filter or your array for pagination

        foreach($items as $item){
            if($item['id']>40 && $item['id']<50){
                array_push($filter_products, $item);
            }
        }

        $count = count($filter_products); // total product for pagination
        $page = $request->page; // current page for pagination

        // manually slice array of product to display on page
        $perPage = 5;
        $offset = ($page-1) * $perPage;
        $products = array_slice($filter_products, $offset, $perPage);

        // your pagination 
        $products = new Paginator($products, $count, $perPage, $page, ['path'  => $request->url(),'query' => $request->query(),]);
        // use {{ $products->appends($_GET)->links() }} to dispaly your pagination
        return view('index',['products' => $products]);
    }
}
Sangeet
  • 21
  • 4