dimanche 29 novembre 2020

How to paginate the API response of another API?

The context: Laravel 5.7, ApiClient for external client available via ServiceProvider & Facade. I get the response with the list of dictionaries in the controller. Is there a way to paginate the API response of another API in the view to get another portion?

For example, in this code:

public function report(Request $request) {
        $date_from = $request->date_from;
        $date_to = $request->date_to;

        $func = function($date) {
            return date("Y-m-d", strtotime($date));
        };


        if(!empty($date_from) && !empty($date_to)) {
            $date_from_slice = explode(' - ', $date_from);
            $date_from_slice = array_map($func, $date_from_slice);

            $stats = [];
            $data = [
                'from'       => $date_from_slice[0],
                'to'         => $date_from_slice[1],
                'page'       => 1,
                'rowsOnPage' => 20,
                'groupBy[4]' => 'sspId',
            ];

            $reportDataFirst = ApiClient::exec('statistics', $data);
            $first_collection = collect($reportDataFirst->data);

            if(!empty($first_collection)){
                foreach($first_collection as $first_item) {
                        $stats[] = [
                            'ssp_id'                 => $first_item->sspId,
                            'ssp_name'               => $first_item->sspName,
                            'impressions_first'      => $first_item->impressions,
                            'ssp_spend_first'        => $first_item->sspSpend,
                            'dsp_spend_first'        => $first_item->dspSpend,
                         ];
                 }
             }
            $data = ['stats' => $stats];
            return view('client.index', $data);
}

I have fixed 'page' and 'rowsOnPage' parameters:

            'page'       => 1,
            'rowsOnPage' => 20,

I need something to create the pagination interface for it. Is there a way to build it in Laravel?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire