@@ -170,8 +170,8 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra
170170 */
171171 public function success ($ data = null , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
172172 {
173- if ($ data instanceof ResourceCollection && ( $ data -> resource instanceof AbstractPaginator) ) {
174- return $ this ->formatPaginatedResourceResponse (...func_get_args ());
173+ if ($ data instanceof ResourceCollection) {
174+ return $ this ->formatResourceCollectionResponse (...func_get_args ());
175175 }
176176
177177 if ($ data instanceof JsonResource) {
@@ -305,7 +305,7 @@ protected function formatPaginatedData(array $paginated)
305305 }
306306
307307 /**
308- * Format paginated resource response.
308+ * Format collection resource response.
309309 *
310310 * @param JsonResource $resource
311311 * @param string $message
@@ -315,16 +315,17 @@ protected function formatPaginatedData(array $paginated)
315315 *
316316 * @return mixed
317317 */
318- protected function formatPaginatedResourceResponse ($ resource , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
318+ protected function formatResourceCollectionResponse ($ resource , string $ message = '' , $ code = 200 , array $ headers = [], $ option = 0 )
319319 {
320- $ paginated = $ resource -> resource -> toArray ( );
320+ $ dataField = Config:: get ( ' response.format.paginated_resource.data_field ' , ' data ' );
321321
322- $ paginationInformation = $ this -> formatPaginatedData ( $ paginated );
323-
324- $ paginationDataField = Config:: get ( ' response.format.paginated_resource.data_field ' , ' data ' );
325- $ resourceData = array_merge_recursive ([ $ paginationDataField => $ resource -> resolve ( request ())], $ resource -> with ( request ()), $ resource -> additional );
322+ $ data = array_merge_recursive ([ $ dataField => $ resource -> resolve ( request ())], $ resource -> with ( request ()), $ resource -> additional );
323+ if ( $ resource -> resource instanceof AbstractPaginator) {
324+ $ paginated = $ resource -> resource -> toArray ( );
325+ $ paginationInformation = $ this -> formatPaginatedData ( $ paginated );
326326
327- $ data = array_merge_recursive ($ resourceData , $ paginationInformation );
327+ $ data = array_merge_recursive ($ data , $ paginationInformation );
328+ }
328329
329330 return tap (
330331 $ this ->response ($ this ->formatData ($ data , $ message , $ code ), $ code , $ headers , $ option ),
0 commit comments