{"id":15067174,"url":"https://github.com/hakobyansen/laravel-response-builder","last_synced_at":"2025-04-10T13:54:48.645Z","repository":{"id":41852864,"uuid":"148393164","full_name":"hakobyansen/laravel-response-builder","owner":"hakobyansen","description":"This is a simple package for Laravel framework to standardize the structure of JSON responses.","archived":false,"fork":false,"pushed_at":"2023-09-15T07:29:14.000Z","size":218,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-24T12:39:32.738Z","etag":null,"topics":["json-responses","laravel","laravel-framework","laravel-response-builder"],"latest_commit_sha":null,"homepage":"","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/hakobyansen.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2018-09-11T23:34:32.000Z","updated_at":"2023-03-02T09:43:45.000Z","dependencies_parsed_at":"2024-09-29T11:41:41.720Z","dependency_job_id":"8ab65786-4234-4c5f-a3f9-fee4335fd961","html_url":"https://github.com/hakobyansen/laravel-response-builder","commit_stats":{"total_commits":144,"total_committers":5,"mean_commits":28.8,"dds":0.1527777777777778,"last_synced_commit":"5867ec87c535776031ff5b1c56d044fe99f263ab"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakobyansen%2Flaravel-response-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakobyansen%2Flaravel-response-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakobyansen%2Flaravel-response-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakobyansen%2Flaravel-response-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hakobyansen","download_url":"https://codeload.github.com/hakobyansen/laravel-response-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248229461,"owners_count":21068900,"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":["json-responses","laravel","laravel-framework","laravel-response-builder"],"created_at":"2024-09-25T01:17:34.745Z","updated_at":"2025-04-10T13:54:48.624Z","avatar_url":"https://github.com/hakobyansen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## About\nPackage `codebot/laravel-response-builder` is a simple package for Laravel framework to standardize the structure of JSON responses.\n\nYou will find this package helpful if you need your responses to look like this:\n\n```json\n{\n  \"status\": true,\n  \"message\": \"You got that!\",\n  \"data\" : {\n    \"name\": \"James Joesph Bulger\", \n    \"profession\": \"criminal\"\n  },\n  \"errors\": []\n}\n```\n\nor\n\n```json\n{\n  \"status\": false,\n  \"message\": \"Something went wrong...\",\n  \"data\" : [],\n  \"errors\": {\n    \"email\": [\n      \"The email field is required.\"\n    ]\n  }\n}\n```\n\n## Installation  \n```\ncomposer require codebot/laravel-response-builder:^1\n```\n\n**For Laravel 5.4 and versions below add `\\Rb\\Core\\RbServiceProvider::class` to providers in config/app.php file.**\n\nNext run the command below in your console:\n\n```\nphp artisan vendor:publish --tag=laravel-response-builder\n```\n\n## Requests\n\nOnce you published vendor, you should see `App\\Http\\Requests\\Rb\\RbRequest class`. This will be base class for\nlaravel-response-builder's requests.  \nTo generate a request that extends RbRequest, run:\n  \n```\nphp artisan make:rbrequest YourNewRequest\n``` \n\nIf you are using Laravel's Validator class, you can use `Rb\\Core\\RbValidator` to standardize the structure of failed response. \n\n```php\nuse \\Rb\\Core\\RbValidator;\nuse Illuminate\\Support\\Facades\\Validator;\n\n$validator = new Validator($data, $rules); // assuming you have a $validator instance\n\nRbValidator::validate($validator); // throws HttpResponseException or returns boolean true \n```\n\nRbValidator::validate() method checks if the validation fails and throws Illuminate\\Http\\Exceptions\\HttpResponseException with the standardized json response structure.\n\n### Usage  \n```php\nuse \\Rb\\Core\\Response;\nuse \\Rb\\Core\\HttpStatusCode;\n\n$response = new Response();\n\n$response-\u003esetStatusCode(HttpStatusCode::OK); // required. If code is 2XX then Response::status field will be \"true\", otherwise \"false\"\n\n$response-\u003esetMessage('Some inspiring message.'); // null will be returned if no message set\n\n$response-\u003esetData($data); // null will be returned if no data set\n\n$response-\u003esetErrors($errors); // null will be returned if no error set\n\n$response-\u003egetArray(); // will return an array of data set\n\n$response-\u003egetResponse(); // will return a json response using Laravel's response() helper \n```\n\nAll setters are fluent, so example above could be written like:\n```php\nuse \\Rb\\Core\\Response;\nuse \\Rb\\Core\\HttpStatusCode;\n\n$response = new Response();\n\n$response-\u003esetStatusCode(HttpStatusCode::OK)\n    -\u003esetMessage('Some inspiring message.'); \n    // ...\n```\n\nPackage contains HttpStatusCode class with http status codes defined.\n\n```php\nuse \\Rb\\Core\\HttpStatusCode;\n\nHttpStatusCode::OK; // returns status code 200 (integer)\n\nHttpStatusCode::getMessageByCode(HttpStatusCode::NOT_FOUND); // returns string \"Not Found\"\n\nHttpStatusCode::getCodeWithMessage(HttpStatusCode::CREATED); // returns string - code and message, e.g. \"201 Created\"\n```\n\n#### The Facade\n\nThe package contains a facade class in case if you don't want to interact with all of these setters.\n\n```php\nuse \\Rb\\Facade\\Response;\nuse \\Rb\\Core\\HttpStatusCode;\n\nreturn Response::success(\n   data: $data,\n   message: 'Created List of users.'\n);\n\nreturn Response::error(\n   errors: $errors,\n   message: 'Invalid input.',\n   statusCode: HttpStatusCode::UNPROCESSABLE_ENTITY\n);\n\n// Without $data (the $errors parameter is also optional)\nreturn Response::success(message: 'User deleted.');\n```\n\n`$data` and `$errors` variables are arrays and are optional.\n\n## Configuration\n\nOnce you have vendor published, you should see `config/response_builder.php` file.\n\n**request_path** - Generated request classes will be stored in specified directory.  \n\n**request_namespace** - Namespace for generated request class.  \n\n**is_authorize** - Default value of Request's authorize() method.\n\n**messages** - Contains messages for Response's \"message\" field.  \n\n*messages.failed_validation* - Default message for response that failed the validation.\n\n## Fields\n\n`status` - boolean (Not Nullable)  \n`status_code` - integer (Not Nullable)  \n`message` - string (Nullable)  \n`data` - array (Not Nullable, can be empty)  \n`errors` - array (Not Nullable, can be empty)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhakobyansen%2Flaravel-response-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhakobyansen%2Flaravel-response-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhakobyansen%2Flaravel-response-builder/lists"}