Response.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Supports;
  3. use Illuminate\Pagination\LengthAwarePaginator;
  4. class Response
  5. {
  6. /**
  7. * response ok
  8. * @param [type] $result [description]
  9. * @param array $custom [description]
  10. * @param integer $responseCode [description]
  11. * @return object [description]
  12. */
  13. public static function ok($result = [], int $responseCode = 200, array $custom = [])
  14. {
  15. $paginator = [];
  16. if ($result instanceof LengthAwarePaginator) {
  17. $paginator = collect($result->toArray());
  18. $result = $paginator->pull('data');
  19. $paginator = ['paginate' => $paginator];
  20. }
  21. $output = array_merge(['data' => $result], $paginator, $custom);
  22. return response()->json($output, $responseCode);
  23. }
  24. /**
  25. * response fail
  26. * @param string $message [description]
  27. * @param array $payload [description]
  28. * @param integer $responseCode [description]
  29. * @return object [description]
  30. */
  31. public static function fail($message = 'api error', int $responseCode = 400, array $payload = [])
  32. {
  33. return response()->json([
  34. 'message' => $message,
  35. 'errors' => $payload,
  36. ], $responseCode);
  37. }
  38. }