tehseen's avatar

I need pagination with chunk

Is that possible to use paginate with chunk i mean the bottom number nav with chunk ?

for example i need 6 item per row with chunk and have total 12 item in page with pagination is that something i can do with laravel for now i can use this below code but not success.

In my controller

public function getShop() {
        
        $products = Product::get()->all();
        return view('frontend/pages/shop')->withProducts($products);
    }

in my view

  @foreach(array_chunk($products,6) as $product)
                    <div class="row mb-4">
                        @foreach($product as $item)
                            <div class="col-md-2">
                                <div class="moveicon-animated-icon" data-icon="{{ asset( 'jsonicons/' . $item->product_jsonicon) }}"></div>
                            </div>
                        @endforeach
                    </div>
                @endforeach

how ever the chunk works fine but who i add the pagination with chunk i am aware the paginate function but it did not work for me

thanks in advance

0 likes
11 replies
Sinnbeck's avatar

Be aware that using pagination will move the contents to ->values()

$products = Product::get()->paginate(12);
        return view('frontend/pages/shop')->withProducts($products);
 @foreach(array_chunk($products->values(),6) as $product)
tehseen's avatar

Thanks @sinnbeck for your reply but i have see the error when i update the code

Method paginate does not exist.

why this ?

tehseen's avatar

i use the above but on my view

  @foreach(array_chunk($products->values(),6) as $product)
                    <div class="row mb-4">
                        @foreach($product as $item)
                            <div class="col-md-2">
                                <a href="#">
                                    <div class="moveicon-animated-icon" data-icon="{{ asset( 'jsonicons/' . $item->product_jsonicon) }}"></div>
                                </a>

                            </div>
                        @endforeach
                    </div>
                @endforeach

i have error

array_chunk() expects parameter 1 to be array, object given (View: C:\xampp\htdocs\MoveiconLaravel\resources\views\frontend\pages\shop.blade.php)

any idea ?

Nakov's avatar

@tehseen I guess that returns a collection.. use this:

$products->values()->toArray()
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Sorry. I of course meant just ->paginate not ->get()->paginate();

Anyways try

@foreach($products->values()->chunk(6) as $product)
Sinnbeck's avatar

Did it work with my answer then or the one by @nakov ? Just curious :)

tehseen's avatar

actually both of yours but i only add one as best answer did i do wrong ?

Sinnbeck's avatar

Ok great. That is good to know.

You can select whichever you find working the best. I was just curious. It helps for when other have the same kind of issue

Nakov's avatar

@tehseen and I agree that @sinnbeck way is the better way, as the array_ helper functions got removed from the core of the framework, so you might need to install an additional dependency to have those. The ->chunk() on the collection is the way to go :)

I just gave you an answer on how to turn a Collection object into array in case you need it in the future :)

Please or to participate in this conversation.