@@ -178,6 +178,10 @@ public function success($data = null, string $message = '', $code = 200, array $
178178 return $ this ->formatResourceResponse (...func_get_args ());
179179 }
180180
181+ if ($ data instanceof AbstractPaginator) {
182+ return $ this ->formatPaginatedResponse (...func_get_args ());
183+ }
184+
181185 if ($ data instanceof Arrayable) {
182186 $ data = $ data ->toArray ();
183187 }
@@ -231,24 +235,43 @@ protected function formatData($data, $message, &$code, $errors = null): array
231235 'code ' => $ originalCode ,
232236 'message ' => $ message ,
233237 'data ' => $ data ?: (object ) $ data ,
234- 'error ' => $ errors ?: (object ) [],
238+ 'error ' => $ errors ?: (object ) [],
235239 ];
236240 }
237241
238242 /**
239- * Format paginated resource data .
243+ * Format paginated response .
240244 *
241- * @param JsonResource $resource
245+ * @param AbstractPaginator $resource
242246 * @param string $message
243247 * @param int $code
244248 * @param array $headers
245249 * @param int $option
246250 *
247251 * @return mixed
248252 */
249- protected function formatPaginatedResourceResponse ($ resource , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
253+ protected function formatPaginatedResponse ($ resource , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
254+ {
255+ $ paginated = $ resource ->toArray ();
256+
257+ $ paginationInformation = $ this ->formatPaginatedData ($ paginated );
258+
259+ $ paginationDataField = Config::get ('response.format.paginated_resource.data_field ' , 'data ' );
260+ $ data = array_merge_recursive ([$ paginationDataField => $ paginated ['data ' ]], $ paginationInformation );
261+
262+ return $ this ->response ($ this ->formatData ($ data , $ message , $ code ), $ code , $ headers , $ option );
263+ }
264+
265+
266+ /**
267+ * Format paginated data.
268+ *
269+ * @param array $paginated
270+ *
271+ * @return array
272+ */
273+ protected function formatPaginatedData (array $ paginated )
250274 {
251- $ paginated = $ resource ->resource ->toArray ();
252275 $ count = $ paginated ['total ' ] ?? null ;
253276 $ totalPages = $ paginated ['last_page ' ] ?? null ;
254277 $ previous = $ paginated ['prev_page_url ' ] ?? null ;
@@ -279,6 +302,26 @@ protected function formatPaginatedResourceResponse($resource, string $message =
279302 ];
280303 }
281304
305+ return $ paginationInformation ;
306+ }
307+
308+ /**
309+ * Format paginated resource response.
310+ *
311+ * @param JsonResource $resource
312+ * @param string $message
313+ * @param int $code
314+ * @param array $headers
315+ * @param int $option
316+ *
317+ * @return mixed
318+ */
319+ protected function formatPaginatedResourceResponse ($ resource , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
320+ {
321+ $ paginated = $ resource ->resource ->toArray ();
322+
323+ $ paginationInformation = $ this ->formatPaginatedData ($ paginated );
324+
282325 $ paginationDataField = Config::get ('response.format.paginated_resource.data_field ' , 'data ' );
283326 $ data = array_merge_recursive ([$ paginationDataField => $ this ->parseDataFrom ($ resource )], $ paginationInformation );
284327
0 commit comments