Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianzofcin committed Jan 17, 2023
1 parent 3748716 commit 6dc652e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Laravel Api Response

## Laravel package for easy formatted api response

**Installation**

```bash
composer require wamesk/laravel-api-response
```

**Usage**

For basic response use class and call response() function and pass status code needed *(default 200)*.

```php
ApiResponse::response(201);
```

You can also pass message in your response by adding `message()` function before response function.

```php
ApiResponse::message('Hello')->response(201);
```

You can pass internal code using `message()` function that helps you find of response in case of error.

```php
ApiResponse::code('1.2.1)->message('Hello')->response(201);
```

You can pass data using `data()` function.

```php
ApiResponse::data(['id' => 1, 'name' => 'Jhon Jhonson'])->code('1.2.1)->message('Hello')->response(201);
```

In case you need pagination in your api you can use `collection()` function instead of `data()` function.
You can use this function by passing paginated data, and you can also pass Resource for better data formatting *(Resource is not required)*

```php
$users = User::paginate(10);
ApiResponse::collection($users, UserResource::class)->code('1.2.1)->message('Hello')->response(201);
```
12 changes: 6 additions & 6 deletions src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

namespace Wame\ApiResponse;

class ApiResponse
{
Expand All @@ -24,7 +25,7 @@ class ApiResponse
* @param string $code
* @return static
*/
public static function code(string $code)
public static function code(string $code): static
{
static::$code = $code;

Expand All @@ -37,7 +38,7 @@ public static function code(string $code)
* @param mixed $data
* @return static
*/
public static function data(mixed $data)
public static function data(mixed $data): static
{
static::$data = $data;

Expand Down Expand Up @@ -65,7 +66,7 @@ public static function collection(mixed $data, $resource = null): static
* @param string $message
* @return static
*/
public static function message(string $message)
public static function message(string $message): static
{
static::$message = $message;

Expand All @@ -76,10 +77,9 @@ public static function message(string $message)
* Response :D
*
* @param int $statusCode
* @return \Illuminate\Http\Response|mixed
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @return \Illuminate\Http\JsonResponse
*/
public static function response(int $statusCode)
public static function response(int $statusCode = 200): \Illuminate\Http\JsonResponse
{
$response = collect( self::$data);
$response = $response->merge([
Expand Down

0 comments on commit 6dc652e

Please sign in to comment.