muhammadsaim's avatar

Pagination in Laravel Collection

I have a function which return the array of resource and collection

/**
     *
     * get the brand
     *
     * @param Brand $brand
     * @return JsonResponse
     */
    public function show(Brand $brand) {
        $brand = QueryBuilder::for(Brand::class)
                                ->with([
                                    'products'
                                ])
                                ->find($brand->id);
        $products = QueryBuilder::for(Product::class)->with(['brand', 'registrant', 'productType', 'director', 'publisher'])
                            ->where('brand_id', $brand->id)
                            ->paginate(12, ['*'], 'product_page');

//        return new ProductCollection($products);
        return response()->json([
            'data' => [
                'brand'     => new BrandResource($brand),
                'products'  => new ProductCollection($products)
            ]
        ]);
    }

If I run this then the products have only 12 records but there is 3K+ records when I uncomment the ProductCollection and comment the response underneath or more I just return the collection without wrapping in an Array it contains the pagination.

0 likes
4 replies
jlrdw's avatar

This is an api?

Edit:

For a collection (different from an eloquent collection) you can use a lengthaware paginator and slice. But best to let the database and sql do the work here. Too large of a array it's not good to paginate.

muhammadsaim's avatar

@jlrdw yes, this is an API if I add links and meta key with arrays then it with directly return collection duplicate entries.

jlrdw's avatar

@muhammadsaim Try paginating this query in non api code, not json, in a foreach, does it paginate correctly?

It has to know the next offset for paginating. I suggest also a toSql() to see the actual sql used.

You can try a length aware paginator as well.

Also throw in a page number and see what happens. I normally handle pagination in the javascript and use skip and take so the next run will know the offset.

Edit:

https://laracasts.com/discuss/channels/vue/paginating-from-my-response

jlrdw's avatar

@muhammadsaim Also the key is in the api have a way and instructions for the developer who uses your api to be able to use limit and offset.

Edit:

Go here https://www.petfinder.com/developers/v2/docs/ and search the page for pagination

The query has to be written where a developer can use common things.

Example"

quote

    "pagination": {
        "count_per_page": 20,
        "total_count": 1,
        "current_page": 1,
        "total_pages": 1,
        "_links": {
            "previous": {
                "href": "/v2/animals?type=dog&page=1"
            },
            "next": {
                "href": "/v2/animals?type=dog&page=3"
            }
        }
    }

unquote

Edit:

Try to have straight forward results in an api if possible, meaning keep nested results to a minimum when possible.

Please or to participate in this conversation.