PHPackages                             egeatech/laravel-responses - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. egeatech/laravel-responses

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

egeatech/laravel-responses
==========================

A simple package to better handle application responses.

4.0.1(2y ago)14.0k[2 PRs](https://github.com/EgeatechSRL/laravel-responses/pulls)MITPHPPHP ^8.0

Since May 16Pushed 2y ago4 watchersCompare

[ Source](https://github.com/EgeatechSRL/laravel-responses)[ Packagist](https://packagist.org/packages/egeatech/laravel-responses)[ Docs](https://github.com/EgeatechSRL/laravel-responses)[ RSS](/packages/egeatech-laravel-responses/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (17)Used By (0)

Laravel Responses
=================

[](#laravel-responses)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a5b507b5684fae3c498bd84abb764dffb889f479b3f5c64b7b448538ad67c80a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65676561746563682f6c61726176656c2d726573706f6e7365732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/egeatech/laravel-responses)[![Total Downloads](https://camo.githubusercontent.com/3d23238cfae6e0659d24cdde5417ee9ac9106ec4f17bcf4ab6ec98304dbb98fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65676561746563682f6c61726176656c2d726573706f6e7365732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/egeatech/laravel-responses)

A package to provide some standardized response classes.

Installation
------------

[](#installation)

This package supports Laravel 6, 7, 8 and 9 but requires **at least** PHP 8.0. PHP 7.4 is supported up to version 2.1.1.

Via Composer

```
$ composer require egeatech/laravel-responses
```

Usage
-----

[](#usage)

Simply return a new instance of either `ApiResponse` or `PaginatedApiResponse` from your controller method.

Both classes share a similar signature for their constructors:

Here is the constructor for `ApiResponse`:

```
public function __construct(
    # Either null, a valid Eloquent model or an \Illuminate\Support\Collection instance or a generic payload
    mixed $responseData,

    # A classFQN extending Laravel JsonResource instance, to be used to know how to map response data. Can be null if data don't need formatting.
    ?string $responseFormatter,

    # A valid HTTP status code, to be returned to the caller
    int $httpStatus = \Illuminate\Http\JsonResponse::HTTP_OK,

    # An optional ApplicationException instance, to properly provide a valid error message representation
    ?\EgeaTech\LaravelExceptions\Interfaces\Exceptions\LogicErrorException $logicException = null
) {}
```

And here the one for `PaginatedApiResponse` class:

```
public function __construct(
    # A standard Laravel paginator instance, holding both data and pagination information
    \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginatorData,

    # A classFQN extending Laravel JsonResource instance, to be used to know how to map response data
    string $responseFormatter,

    # A valid HTTP status code, to be returned to the caller
    int $httpStatus = \Illuminate\Http\JsonResponse::HTTP_OK,

    # An optional ApplicationException instance, to properly provide a valid error message representation
    ?\EgeaTech\LaravelExceptions\Interfaces\Exceptions\LogicErrorException $logicException = null
) {}
```

An example usage for the `ApiResponse` class could be the following:

```
