mustafaabdujalil's avatar

Custom Pagination

when i try to create pagination for array of items in the first page it will return well but if i try to get page = 2 or 3 or .... return data of objects like this example

            "current_page": 2,
            "data": {
                "10": {
                    "rate_plane_code": "JOY5zVsquIXP651ubmI2ZRCHn+60VkMy6sCEnIonI69vEHWz+z7cpv7Ela//LGF/mVqWW107s29PAzvpSk7T1lyspOggOTXgSL+dEkUKFvy/JLceRitWPv/E3xYavCN37hNYLpEnAKJ4CEU9lcdMSgU/4X9UmeO2H33ZK+xeIczOHDQO0SxyxtfGCuh8yrQuY9pGpaJHTrqnShhlr3AF42yrSoZcESTDMr8MlVFGklJ2SpI0xjQ1/lCnGZHwWVfzSdwfL/UVTGKw6XPDRbuHEALWejtNCid/SAoeh8YqZ6GnUwUJ22JNWV10HKcOc7G0mGEs5Cq+3LJxLVfn+5pvQJEVEBEEOUh6t3McwvXw2XIy1cybjnH/ayupC6AyoQ2ADF7TwJqEZXH9TfkWWExruvJTvDqNo8U/8v5ug3d7GbjrETpkIo0KRMP57kWJ2xAGTe0AFmOgRGH6vVxqxEXHtrygOWdzHg+1/GSgl31mr7T7sKPFful1qbR0K5qBvxd/szLVAyFfJUESga2EywPXRbwOdnLa5BmTKa5P2A+Eh84cknSZQ5H0ER5gn3Bkk0n94NCE89Q9UNNsZewWOznXsg==",
                    "board": "ROOM ONLY",
                    "price": "198.9",
                    "currency": "EUR",
                    "description": null,
                    "name": [
                        "SINGLE STANDARD"
                    ],
                    "offer_name": null,
                    "offer_amount": null,
                    "offer_cuurency": null,
                    "offer_start_date": null,
                    "offer_end_date": null,
                    "images": [
                        "http://optimum-website-api.test/storage/settings/1585058240.jpg"
                    ]
                },
                "11": {
                    "rate_plane_code": "JOY5zVsquIXP651ubmI2ZRCHn+60VkMy6sCEnIonI69vEHWz+z7cpv7Ela//LGF/mVqWW107s29PAzvpSk7T1lyspOggOTXgSL+dEkUKFvy/JLceRitWPv/E3xYavCN37hNYLpEnAKJ4CEU9lcdMSgU/4X9UmeO2H33ZK+xeIczOHDQO0SxyxtfGCuh8yrQuY9pGpaJHTrqnShhlr3AF42yrSoZcESTDMr8MlVFGklJ2SpI0xjQ1/lCnGZHwWVfzi2jUF2epN+evwxqg3uCvWCN4eYr+LMzIBCbqi4bwsMMuxeVtd0FzY57jD47rMXIZbNbFh6n2tfdR2n0vaUEaOuWlSU55auoFwgCTVBpfSFDKHUV5DqNsBdA7hJZrzaVXQMBuHuLL2MdPcWzOAR/Nt9+VxKPjSJnsuXMa8P+URJmNni3rjgyn/nM2x+4qpi2dbaiuRI7SNmghvFs1ZVPC/6dO6lqLPZyo3xn5+9S2tOCPJRUgPux+H48EsChl85NWo2mHv75ltcmi71F5QeInXlQjWSQqo3iGBdKqQ2Z4GvL+hWMyHZIu6OHpMITZqVBttIZklSHBIOnhX+c98mEXNg==",
                    "board": "ROOM ONLY",
                    "price": "198.9",
                    "currency": "EUR",
                    "description": null,
                    "name": [
                        "SINGLE STANDARD"
                    ],
                    "offer_name": null,
                    "offer_amount": null,
                    "offer_cuurency": null,
                    "offer_start_date": null,
                    "offer_end_date": null,
                    "images": [
                        "http://optimum-website-api.test/storage/settings/1585058240.jpg"
                    ]
                },

i want it to be array of objects not objects of objects

0 likes
1 reply
mustafaabdujalil's avatar
Level 2

I solved it by this code

   public static function customPaginate($data)
    {
        //Get current page form url e.g. &page=6
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        //Create a new Laravel collection from the array data
        $collection = new Collection($data);

        //Define how many items we want to be visible in each page
        $per_page = Config::PAGINATIONCOUNT;

        //Slice the collection to get the items to display in current page
        $currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->values();

        //Create our paginator and add it to the data array
        $data['results'] = new LengthAwarePaginator($currentPageResults, count($collection), $per_page);

        //Set base url for pagination links to follow e.g custom/url?page=6
        return $data['results']->setPath(request()->url());
    }

Thanks to this guy https://stackoverflow.com/questions/53929331/laravel-custom-pagination-on-page-2-return-as-object

Please or to participate in this conversation.