https://github.com/imdhemy/jsonable
Laravel JSON response trait.
https://github.com/imdhemy/jsonable
json json-api laravel laravel-package response
Last synced: 2 days ago
JSON representation
Laravel JSON response trait.
- Host: GitHub
- URL: https://github.com/imdhemy/jsonable
- Owner: imdhemy
- License: mit
- Archived: true
- Created: 2019-03-15T00:08:06.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-07T16:05:05.000Z (over 6 years ago)
- Last Synced: 2025-10-22T13:27:50.291Z (3 months ago)
- Topics: json, json-api, laravel, laravel-package, response
- Language: PHP
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jsonable
Laravel JSON response trait. This trait makes it easy for any controller to return a JSON response with the appropriate HTTP status code.
## Installation
Via composer:
```
composer require imdhemy/jsonable
```
# Usage
All that you need is to `use` the `Jsonable` trait inside your controller.
**Example:**
```php
ok($data, 'countries');
}
}
```
The previous code will return a JSON response like the following:
```json
{
"countries": [
{
"name": "Egypt",
"capital": "Cairo",
},
...
]
}
```
The parent key is optional, you can ommit it:
```php
$data = \App\Country::get();
return $this->ok($data);
```
The response will be like the following:
```json
[
{
"name": "Egypt",
"capital": "Cairo",
},
...
]
```
## Available methods
### Success Methods
| Method | Status code | Description |
|---|---|---|
|ok|200|Successful get, patch (return a JSON object)|
|created|201|Successful post (return a JSON object)|
|noContent|204|Successful delete|
### Error Status
| Method | Status code | Description |
|---|---|---|
|unauthorized|401|Not authenticated|
|invalid|403|Authenticated, but no permissions|
|notFound|404|Not Found|
|invalid|422|Validation|
### Extra methods
| Method | Status code | Description |
|---|---|---|
|accepted|202|Successful post, delete, path - async|
|badRequest|400|The request could not be understood by the server due to malformed syntax|
|paymentRequired|402|Payment required|