@@ -178,6 +178,10 @@ public function success($data = null, string $message = '', $code = 200, array $
178
178
return $ this ->formatResourceResponse (...func_get_args ());
179
179
}
180
180
181
+ if ($ data instanceof AbstractPaginator) {
182
+ return $ this ->formatPaginatedResponse (...func_get_args ());
183
+ }
184
+
181
185
if ($ data instanceof Arrayable) {
182
186
$ data = $ data ->toArray ();
183
187
}
@@ -231,24 +235,43 @@ protected function formatData($data, $message, &$code, $errors = null): array
231
235
'code ' => $ originalCode ,
232
236
'message ' => $ message ,
233
237
'data ' => $ data ?: (object ) $ data ,
234
- 'error ' => $ errors ?: (object ) [],
238
+ 'error ' => $ errors ?: (object ) [],
235
239
];
236
240
}
237
241
238
242
/**
239
- * Format paginated resource data .
243
+ * Format paginated response .
240
244
*
241
- * @param JsonResource $resource
245
+ * @param AbstractPaginator $resource
242
246
* @param string $message
243
247
* @param int $code
244
248
* @param array $headers
245
249
* @param int $option
246
250
*
247
251
* @return mixed
248
252
*/
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 )
250
274
{
251
- $ paginated = $ resource ->resource ->toArray ();
252
275
$ count = $ paginated ['total ' ] ?? null ;
253
276
$ totalPages = $ paginated ['last_page ' ] ?? null ;
254
277
$ previous = $ paginated ['prev_page_url ' ] ?? null ;
@@ -279,6 +302,26 @@ protected function formatPaginatedResourceResponse($resource, string $message =
279
302
];
280
303
}
281
304
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
+
282
325
$ paginationDataField = Config::get ('response.format.paginated_resource.data_field ' , 'data ' );
283
326
$ data = array_merge_recursive ([$ paginationDataField => $ this ->parseDataFrom ($ resource )], $ paginationInformation );
284
327
0 commit comments