https://github.com/djurovicigoor/lara-json-response
Laravel API wrapper for returning JSON response.
https://github.com/djurovicigoor/lara-json-response
api json laravel response
Last synced: 7 months ago
JSON representation
Laravel API wrapper for returning JSON response.
- Host: GitHub
- URL: https://github.com/djurovicigoor/lara-json-response
- Owner: djurovicigoor
- License: other
- Created: 2018-09-06T12:30:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-14T14:59:35.000Z (almost 6 years ago)
- Last Synced: 2024-08-10T23:43:53.437Z (almost 2 years ago)
- Topics: api, json, laravel, response
- Language: PHP
- Homepage:
- Size: 26.4 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- Contributing: contributing.md
- License: license.md
Awesome Lists containing this project
README
# LaraJsonResponse
[](https://packagist.org/packages/djurovicigoor/larajsonresponse)

[](https://opensource.org/licenses/MIT)


Laravel API wrapper for returning JSON response.
## Installation
Via Composer
``` bash
$ composer require djurovicigoor/larajsonresponse
```
## Usage
The base method is **laraResponse()**;
``` bash
return laraResponse();
```
JSON response of this method will be:
``` bash
{
"code": 200,
"message": null,
"data": null,
"error": null
}
```
If you want to return success JSON response with data and status code 200, you have to use **laraResponse($message , $data)->success()**
``` bash
$data = ['name' => 'John Doe', 'location' => 'unknown' , 'age' => 24];
return laraResponse("My message." , $data)->success();
```
JSON response of this method will be:
``` bash
{
"code": 200,
"message": "My message.",
"data": {
"name": "John Doe",
"location": "unknown",
"age": 24
},
"error": null
}
```
If you want to return error JSON response with message, ERROR and status code 400, you have to use **laraResponse($message , NULL , $error)->error()**
``` bash
return laraResponse('My message.' , NULL , "TYPE_OF_ERROR")->error();
```
JSON response of this method will be:
``` bash
{
"code": 400,
"message": "My message.",
"data": null,
"error": "TYPE_OF_ERROR"
}
```
If you want to return unauthorized JSON response response with 401 status code, you have to use **laraResponse($message)->unAuthorized()**
``` bash
return laraResponse('This action is unauthorized.')->unAuthorized();
```
JSON response of this method will be:
``` bash
{
"code": 401,
"message": "This action is unauthorized.",
"data": null,
"error": null
}
```
If you want to return forbidden JSON response response with 403 status code, you have to use **laraResponse($message)->forbidden()**
``` bash
return laraResponse('Forbidden!')->forbidden();
```
JSON response of this method will be:
``` bash
{
"code": 403,
"message": "Forbidden!",
"data": null,
"error": null
}
```
If you want to return not found JSON response with 404 status code , you have to use **laraResponse($message)->notFound()**
``` bash
return laraResponse('Not Found!')->notFound();
```
JSON response of this method will be:
``` bash
{
"code": 404,
"message": "Not Found!",
"data": null,
"error": null
}
```
And finaly you can use **laraResponse($message)->customCode($yourCustomCode)** to return JSON response with yout custom code;
``` bash
return laraResponse('My custom status code!')->customCode($yourCustomCode);
```
JSON response of this method will be:
``` bash
{
"code": $yourCustomCode,
"message": "My custom status code!",
"data": null,
"error": null
}
```
## Change log
Please see the [changelog](changelog.md) for more information on what has changed recently.
## Contributing
Please see [contributing.md](contributing.md) for details and a todolist.
## Security
If you discover any security related issues, please email djurovic.igoor@gmail.com instead of using the issue tracker.
## Donate
If you found this project helpful or you learned something from the source code and want to appreciate
- [PayPal](https://paypal.me/djurovicigoor?locale.x=en_US)
## Credits
- [Djurovic Igor][link-author]
## License
MIT. Please see the [license file](license.md) for more information.
[ico-version]: https://img.shields.io/packagist/v/djurovicigoor/larajsonresponse.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/djurovicigoor/larajsonresponse.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/djurovicigoor/larajsonresponse/master.svg?style=flat-square
[ico-styleci]: https://styleci.io/repos/12345678/shield
[link-packagist]: https://packagist.org/packages/djurovicigoor/larajsonresponse
[link-downloads]: https://packagist.org/packages/djurovicigoor/larajsonresponse
[link-travis]: https://travis-ci.org/djurovicigoor/larajsonresponse
[link-styleci]: https://styleci.io/repos/12345678
[link-author]: https://github.com/djurovicigoor
[link-contributors]: ../../contributors]