0

Pagination in laravel is showing blank page in pages other than first.

below is controller

$products=DB::table('products')->join('mobile_devices', 'products.id', '=', 'mobile_devices.product_id')->where('mobile_devices.brand', '=',$request->brand)
            ->where('mobile_devices.ram', '=', $request->ram)
            ->where('mobile_devices.storage', '=',$request->storage )
            ->select('products.*')->paginate(15);

below is blade
<div class="product-filter product-filter-bottom filters-panel">
                    <div class="row">
                        <div class="col-sm-6 text-left"></div>
                        <div class="col-sm-6 text-right">{{$products->links()}}</div>
                    </div>
                </div>

working fine in eloquent result but in query result the problem is occuring. The first page is shown but in other pages just blank page appears
Pemba Tamang
  • 166
  • 1
  • 3
  • 14

2 Answers2

1

Do the properties request->ram and $request->storage carry over to the pagination links? They're probably not if you're not adding them in. See "Appending to Pagination Links" in the docs at https://laravel.com/docs/6.x/pagination#displaying-pagination-results

$products = DB::table(...);
$products->appends([
    'ram' => $request->ram, 
    'storage' => $request->storage,
    'brand_id' => $request->brand_id,
    'brand' => $request->brand
]);

See also How to automatically append query string to laravel pagination links?

Erin
  • 5,315
  • 2
  • 20
  • 36
  • ya ram,storage have to passed to next page.i did it {{ $users->appends(['sort' => 'votes'])->links() }} this way following documentation but still problem is not solved. – Pemba Tamang Oct 23 '19 at 06:52
  • i got this problem after filter products page..the url looks like this http://primestorenepal.ga/filter-listing-products?brand_id=6&brand=samsung&ram=&storage= .This is the first page – Pemba Tamang Oct 23 '19 at 06:53
  • On your site, the URL for the next link is "http://primestorenepal.ga/filter-listing-products?page=2". The vars are NOT in the query string. It should be "http://primestorenepal.ga/filter-listing-products?brand_id=6&brand=samsung&ram=&storage=&page=2" – Erin Oct 23 '19 at 10:58
  • Looks like your controller breaks if you don't have both the `brand_id` and the `brand` query var filled on request. – Erin Oct 23 '19 at 11:03
  • i did it as {!! $products->appends(Request::except('page'))->render() !!} and it works...thanks for the instant response – Pemba Tamang Oct 23 '19 at 16:56
0

In your view,use the collection/result like this..

 @foreach($products as $product)
   {{$product->name}} // whatever you want to display
 @endforeach
 {{$products->links()}} // use the exact name of the object that you are passed to the view
Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17