You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

2 weeks ago
<?php
namespace App\Support;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\JsonResponse;
trait ApiResponse
{
protected function ok(mixed $data = null, string $message = 'ok'): JsonResponse
{
return response()->json([
'message' => $message,
'data' => $data,
]);
}
protected function fail(string $message, int $status = 422, mixed $data = null): JsonResponse
{
return response()->json([
'message' => $message,
'data' => $data,
], $status);
}
protected function paginated(LengthAwarePaginator $paginator, string $message = 'ok'): JsonResponse
{
return response()->json([
'message' => $message,
'data' => [
'items' => $paginator->items(),
'meta' => [
'current_page' => $paginator->currentPage(),
'per_page' => $paginator->perPage(),
'total' => $paginator->total(),
'last_page' => $paginator->lastPage(),
],
],
]);
}
}