{"id":19511285,"url":"https://github.com/jornatf/laravel-api-json-response","last_synced_at":"2026-05-09T06:36:01.353Z","repository":{"id":153947375,"uuid":"631183486","full_name":"jornatf/laravel-api-json-response","owner":"jornatf","description":"A Laravel Package that returns a JSON Response for APIs.","archived":false,"fork":false,"pushed_at":"2023-10-09T16:09:54.000Z","size":43,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-08T11:48:22.843Z","etag":null,"topics":["api","json","json-api","laravel","laravel-package","php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/jornatf/laravel-api-json-response","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jornatf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-22T07:42:21.000Z","updated_at":"2023-05-04T16:49:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"d9221fc1-58ff-4fdf-a939-8899eb9cc85e","html_url":"https://github.com/jornatf/laravel-api-json-response","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jornatf%2Flaravel-api-json-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jornatf%2Flaravel-api-json-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jornatf%2Flaravel-api-json-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jornatf%2Flaravel-api-json-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jornatf","download_url":"https://codeload.github.com/jornatf/laravel-api-json-response/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240761142,"owners_count":19853256,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","json","json-api","laravel","laravel-package","php"],"created_at":"2024-11-10T23:20:11.970Z","updated_at":"2026-05-09T06:35:56.322Z","avatar_url":"https://github.com/jornatf.png","language":"PHP","funding_links":["https://www.buymeacoffee.com/jornatf"],"categories":[],"sub_categories":[],"readme":"# Laravel API Json Response\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/jornatf/laravel-api-json-response.svg?style=flat-square)](https://packagist.org/packages/jornatf/laravel-api-json-response)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/jornatf/laravel-api-json-response/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/jornatf/laravel-api-json-response/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/jornatf/laravel-api-json-response/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/jornatf/laravel-api-json-response/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/jornatf/laravel-api-json-response.svg?style=flat-square)](https://packagist.org/packages/jornatf/laravel-api-json-response)\n\nA Laravel Package that returns a JSON Response for APIs. In some methods, you can return cool Json response for your API.\n\n\u003e #### If you like this package you can [Buy me a Coffee](https://www.buymeacoffee.com/jornatf) ☕️\n\n## Installation\n\n### Via composer:\n\n```bash\ncomposer require jornatf/laravel-api-json-response\n```\n\n## Usage\n\n### Example 1:\n\n\u003e This example shows you how to use the basic required methods.\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Models\\Post;\nuse App\\Http\\Controllers\\Controller;\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\Validator;\n\nclass PostController extends Controller\n{\n    public function store(Request $request)\n    {\n        $validator = Validator::make($request-\u003eall(), [\n            'title' =\u003e 'required|unique:posts|max:255',\n            'body' =\u003e 'required',\n        ]);\n\n        if ($validator-\u003efails()) {\n            // Status code 400 for \"Bad Request\"\n            return ApiResponse::response(400)\n                -\u003eaddDetails($validator-\u003eerrors())\n                -\u003ejson();\n        }\n\n        $post = Post::create($validator-\u003evalidated());\n\n        // Status code 200 for \"Ok\" or 201 for \"Created\"\n        return ApiResponse::response(201)\n            -\u003eaddDatas($post)\n            -\u003ejson();\n    }\n}\n```\n\n**Success response:**\n\n```json\n{\n    \"success\": {\n        \"status\": 201,\n        \"message\": \"Created\",\n        \"data\": {\n            \"id\": 1,\n            \"title\": \"Morbi in diam id dolor vehicula finibus\",\n            \"content\": \"\u003cp\u003eLorem ipsum dolor sit amet, ...\u003c/p\u003e\",\n            \"created_at\": \"2023-04-20 00:00:00\",\n            \"updated_at\": \"2023-04-20 00:00:00\"\n        }\n    }\n}\n```\n\n**Error response:**\n\n```json\n{\n    \"error\": {\n        \"status\": 404,\n        \"message\": \"Not Found\"\n    }\n}\n```\n\n### Exemple 2:\n\n\u003e This example shows you how to use a method to find a model and return a JSON Reponse in a single line of code.\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Models\\Post;\nuse App\\Http\\Controllers\\Controller;\n\nclass PostController extends Controller\n{\n\tpublic function show(int $post_id)\n\t{\n\t\treturn ApiResponse::find(Post::class, $post_id)-\u003ejson();\n\t}\n}\n```\n\n**If model found:**\n\n```json\n{\n    \"success\": {\n        \"status\": 200,\n        \"message\": \"Post Found\",\n        \"data\": {\n            \"id\": 1,\n            \"title\": \"Morbi in diam id dolor vehicula finibus\",\n            \"content\": \"\u003cp\u003eLorem ipsum dolor sit amet, ...\u003c/p\u003e\",\n            \"created_at\": \"2023-04-20 00:00:00\",\n            \"updated_at\": \"2023-04-20 00:00:00\"\n        }\n    }\n}\n```\n\n**Else:**\n\n```json\n{\n    \"error\": {\n        \"status\": 404,\n        \"message\": \"Post Not Found\"\n    }\n}\n```\n\n## Documentation\n\n### Available methods:\n\n```php\n\u003c?php\n\n// First, you can instantiate response with a status code (required):\n$response = ApiResponse::response(int $statusCode);\n// or directly find a model by id:\n$response = ApiResponse::find(Model::class, int $id);\n\n// To add custom message:\n$response-\u003eaddMessage(string $message);\n\n// To add datas to return when success:\n$response-\u003eaddDatas(array $datas);\n\n// To add details (e.g. validator errors) when error:\n$response-\u003eaddDetails(mixed $details);\n// or\n$response-\u003eaddDetails(array $details);\n$response-\u003eaddDetails(array $details);\n\n// Last, formate response into json (required):\n$response-\u003ejson()\n```\n\n### Available status code:\n\n| Status code | Response type | Default message              |\n| ----------- | ------------- | ---------------------------- |\n| `200`       | `success`     | Ok\u003cbr\u003eModel Found            |\n| `201`       | `success`     | Created                      |\n| `202`       | `success`     | Accepted                     |\n| `400`       | `error`       | Bad Request                  |\n| `401`       | `error`       | Unauthorized                 |\n| `403`       | `error`       | Forbidden                    |\n| `404`       | `error`       | Not Found\u003cbr\u003eModel Not Found |\n| `405`       | `error`       | Method Not Allowed           |\n| `408`       | `error`       | Request Timeout              |\n| `429`       | `error`       | Too Many Requests            |\n| `500`       | `error`       | Internal Server Error        |\n| `502`       | `error`       | Bad Gateway                  |\n| `503`       | `error`       | Service Unavailable          |\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\n\u003e Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\n\u003e Feel free to contribute to this project to improve with new features or fix bugs 👍\n\n## Credits\n\n-   [Jordan](https://github.com/jornatf)\n-   [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT).\n\n\u003e Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjornatf%2Flaravel-api-json-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjornatf%2Flaravel-api-json-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjornatf%2Flaravel-api-json-response/lists"}