PHPackages                             harishdpatel/apiresponse - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. harishdpatel/apiresponse

ActiveLibrary[API Development](/categories/api)

harishdpatel/apiresponse
========================

This is common API response structre for laravel based project. It can be used in laravel and lumen

5285PHP

Since Sep 17Pushed 6y ago1 watchersCompare

[ Source](https://github.com/harishpatel143/laravel-api-response)[ Packagist](https://packagist.org/packages/harishdpatel/apiresponse)[ RSS](/packages/harishdpatel-apiresponse/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

laravel-api-response
====================

[](#laravel-api-response)

Laravel API response structure class for json response. In this package, there are many build-in methods for different status code wise response for an API request.

### How to use

[](#how-to-use)

For use this package, You just need to create an object of the **ApiResponse** class

Step -1: In your Controller

```
    public $apiResponse = '';

    /**
     * Create a new controller instance.
     *
     * @param ApiResponse $apiResponseClass
     */
    public function __construct(ApiResponse $apiResponseClass)
    {
        $this->apiResponse = $apiResponseClass;
    }
```

Step-2: Use the **$apiResponse** object for response in your methods.

```
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $posts = Post::get();

        return $this->apiResponse->respondWithMessageAndPayload($posts, 'Posts has been retrieved successfully.');
    }
```

You will receive API response like below

```
{
    "success": true,
    "message": "Posts has been retrieved successfully.",
    "payload": [
        {
            "id": 5,
            "category_id": 1,
            "user_id": 1,
            "title": "Laravel June Chapter2",
            "description": "Laravel June Chapter2",
            "updated_at": "2019-09-11 12:28:02",
        }
    ]
}
```

There are many other use full methods for API response method.

```
return $this->apiResponse->respondWithData(array $data);
return $this->apiResponse->respondWithMessage($message = "Ok");
return $this->apiResponse->respondWithError('Posts has been retrieved successfully');
return $this->apiResponse->respondWithMessageAndPayload($payload = null, $message = "Ok");
return $this->apiResponse->respondWithError($message = "Error", $e = null, $data = null);
return $this->apiResponse->respondOk($message = "Ok");
return $this->apiResponse->respondCreated($message = "Created");
return $this->apiResponse->respondCreatedWithPayload($payload = null, $message = "Created");
return $this->apiResponse->respondUpdated($message = "Updated");
return $this->apiResponse->respondUpdatedWithPayload($payload = null, $message = "Updated");
return $this->apiResponse->respondDeleted($message = "Deleted");
return $this->apiResponse->respondDeletedWithPayload($payload = null, $message = "Deleted");
return $this->apiResponse->respondUnauthorized($message = "Unauthorized");
return $this->apiResponse->respondForbidden($message = "Forbidden");
return $this->apiResponse->respondNotFound($message = "Not Found");
return $this->apiResponse->respondValidationError($message = "Validation Error", $data = null);
return $this->apiResponse->respondInternalError($message, $e);
return $this->apiResponse->respondServiceUnavailable($message, $e);
return $this->apiResponse->respondCustomError($message, $status_code, $e);
return $this->apiResponse->respondNotImplemented($message = "Internal Error");
return $this->apiResponse->respondResourceConflict($message = "Resource Already Exists");
return $this->apiResponse->respondResourceConflictWithData($payload = null, $message = "Resource Already Exists", $responseCode = ResponseHTTP::HTTP_CONFLICT);
return $this->apiResponse->respondWithFile($file, $mime);
return $this->apiResponse->respondNoContent($message);
return $this->apiResponse->respondBadRequest($message = "Bad Request");
return $this->apiResponse->respondHTTPNotAcceptable($message = "HTTP Not Acceptable");
return $this->apiResponse->respondExceptionError($status, $message, $status_code, $payload);
```

**The package has a bellow response structure from all the method**

```

{
    "success": true,
    "message": "",
    "payload": []
}

In Debug mode

{
    "success": false,
    "message": '',
    "payload": '',
    "debug": ''
}

```

**However, if you want to change the response structure or add any new methods in for that does not exist in our package, You just need to create a custom response class and extend ApiResponse class. Override the getResponseStructure in a Custom response class and use that class to the application.**

##### e.g.

[](#eg)

```
namespace App\Http\Response;

use HarishPatel\ApiResponse\ApiResponse;
use Symfony\Component\HttpFoundation\Response;

class CustomApiResponse extends ApiResponse
{

    public function getResponseStructure($success = false, $payload = null, $message = '', $debug = null)
    {
        if ($success) {
            $data = [
                'message' => $message,
                'data' => $payload
            ];
        } else {
            $data = [
                'error' => [
                    'code' => Response::$statusTexts[$this->getStatusCode()],
                    'http_code' => $this->getStatusCode(),
                    'message' => $message,
                ]
            ];
        }

        return $data;
    }
}
```

License
-------

[](#license)

laravel-api-response is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/74aff87f1263b4c87488da7584c51745dc2050eeb393667ae49e26f5bba18ac2?d=identicon)[harishpatel143](/maintainers/harishpatel143)

---

Top Contributors

[![harishpatel-simformsolutions](https://avatars.githubusercontent.com/u/63407804?v=4)](https://github.com/harishpatel-simformsolutions "harishpatel-simformsolutions (6 commits)")

### Embed Badge

![Health badge](/badges/harishdpatel-apiresponse/health.svg)

```
[![Health](https://phpackages.com/badges/harishdpatel-apiresponse/health.svg)](https://phpackages.com/packages/harishdpatel-apiresponse)
```

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35816.3M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24015.5M18](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172437.8k11](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

94452.6k6](/packages/botman-driver-telegram)[emartech/emarsys-magento2-extension

Magento2 integration for the Emarsys Marketing Platform

14273.9k](/packages/emartech-emarsys-magento2-extension)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
