Skip to content

Commit

Permalink
clean response api
Browse files Browse the repository at this point in the history
  • Loading branch information
yaza-putu committed Apr 14, 2024
1 parent 1c5915a commit 2100604
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ With easy repository, you can have the power of the repository pattern, without

## Requirement

- Minimum PHP ^8.1
- Minimum PHP ^8.2

## Installation

Expand All @@ -17,9 +17,13 @@ Specific Version :

| Laravel Version | Package Version |
|:---------------:|:------------------:|
| 10 | 4.x |
| 11 | 5.x |
| 10 | 4.0 |
| 9 | 3.2 |
```bash
# for laravel 10
$ composer require yaza/laravel-repository-service:"^4.0"
# for laravel 9
$ composer require yaza/laravel-repository-service:"^3.2"
```

Expand Down Expand Up @@ -50,8 +54,8 @@ php artisan make:service UserService
// or
php artisan make:service UserService --repository

// create service for api template
php artisan make:service UserService --api
// create service with blank template
php artisan make:service UserService --blank

```

Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ trait Response
* @param null $code
* @return \Illuminate\Http\JsonResponse
*/
public function responseJson($msg = '', $data = [], $code = null, $errors = [])
public function responseJson($msg = '', $data = [], $code = null, $errors = null)
{
if(is_null($code)){
$http_code = 200;
}else{
$http_code = $code;
}

return response()->json([
return response()->json(array_filter([
'code' => $http_code,
'message' => $msg,
'data' => $data,
'errors' => $errors
], $http_code);
]), $http_code);
}
}
6 changes: 3 additions & 3 deletions src/Traits/ResultService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait ResultService
private $data = null;
private $message = null;
private $code = null;
private $errors = [];
private $errors = null;

/**
* set data output
Expand Down Expand Up @@ -149,11 +149,11 @@ public function toJson()
$http_code = $this->getCode();
}

return response()->json([
return response()->json(array_filter([
'code' => $http_code,
'message' => $this->getMessage(),
'data' => $this->getData(),
'errors' => $this->getError(),
], $http_code);
]), $http_code);
}
}

0 comments on commit 2100604

Please sign in to comment.