PHPackages                             ayd/api-response-laravel - 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. ayd/api-response-laravel

ActiveLibrary[API Development](/categories/api)

ayd/api-response-laravel
========================

An API response package for Laravel applications at AYD Company.

v1.0.1(yesterday)01↑2900%MITPHPPHP ^8.1

Since Jun 18Pushed yesterdayCompare

[ Source](https://github.com/AYDcompany/api-response-laravel)[ Packagist](https://packagist.org/packages/ayd/api-response-laravel)[ RSS](/packages/ayd-api-response-laravel/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

API Response Laravel
====================

[](#api-response-laravel)

Laravel integration for the `ayd/api-response-base` package. Provides a service class, trait, facade, and global helper functions for building consistent JSON API responses.

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

[](#installation)

```
composer require ayd/api-response-laravel
```

The service provider is auto-discovered. To register manually:

```
// config/app.php
'providers' => [
    Ayd\ApiResponseLaravel\Providers\ResponseServiceProvider::class,
],
```

Usage
-----

[](#usage)

### 1. Dependency Injection

[](#1-dependency-injection)

Inject `ApiResponse` directly into your controller or service:

```
use Ayd\ApiResponseLaravel\ApiResponse;

class UserController
{
    public function index(ApiResponse $response)
    {
        return $response->success($users);
    }

    public function store(ApiResponse $response)
    {
        // validation...

        return $response->created($user, 'User created');
    }

    public function show(ApiResponse $response, $id)
    {
        $user = User::find($id);

        if (!$user) {
            $response->notFound('User not found'); // throws HttpResponseException
        }

        return $response->success($user);
    }
}
```

### 2. Trait (for Controllers)

[](#2-trait-for-controllers)

Use `ApiResponseTrait` to call response methods directly on the controller:

```
use Ayd\ApiResponseLaravel\ApiResponseTrait;

class UserController
{
    use ApiResponseTrait;

    public function index()
    {
        return $this->success(User::all());
    }

    public function store()
    {
        // validation...

        return $this->created($user, 'User created');
    }

    public function destroy($id)
    {
        User::findOrFail($id)->delete();

        return $this->noContent();
    }
}
```

### 3. Facade

[](#3-facade)

```
use Ayd\ApiResponseLaravel\Facades\Response;

Route::get('/health', fn () => Response::ok(['status' => 'healthy']));
```

To use the facade, add it to `config/app.php`:

```
'aliases' => [
    'ResponseApi' => Ayd\ApiResponseLaravel\Facades\Response::class,
],
```

### 4. Global Helper Functions

[](#4-global-helper-functions)

Helper functions are auto-loaded and return `JsonResponse` directly. Error helpers return the response (they do **not** throw exceptions):

```
return success($data);
return created($user, 'User created');
return updated($user, 'User updated');
return accepted($data);
return no_content();
return message('Processing', 202);
return bad_request('Invalid input', $errors);
return unauthorized();
return forbidden();
return not_found('User not found');
return unprocessable('Validation failed', $errors);
return error('Something went wrong');
return respond($data, 'Custom', 200);
```

Error Handling
--------------

[](#error-handling)

Error methods on the service class and trait **throw `HttpResponseException`** rather than returning a value. This means you can call them without `return`:

```
public function show(ApiResponse $response, $id)
{
    $user = User::find($id);

    if (!$user) {
        $response->notFound('User not found');
        // execution stops here — no return needed
    }

    return $response->success($user);
}
```

Available error methods: `fail()`, `error()`, `badRequest()`, `unauthorized()`, `forbidden()`, `notFound()`, `unprocessable()`.

Paginated Responses
-------------------

[](#paginated-responses)

Transform paginated results with a Laravel API Resource class:

```
public function index(ApiResponse $response)
{
    $paginator = User::paginate(20);

    return $response->paginator($paginator, UserResource::class);
}
```

This automatically:

- Transforms items via `UserResource::collection($paginator->items())->resolve(request())`
- Extracts pagination metadata (`total`, `per_page`, `current_page`) into `meta.pagination`
- Includes `request_id` and any resolved abilities in `meta`

Abilities Resolver
------------------

[](#abilities-resolver)

Bind your resolver in a service provider to auto-inject abilities into every response:

```
use Ayd\ApiResponseBase\Contracts\AbilitiesResolver;

$this->app->bind(AbilitiesResolver::class, MyAbilitiesResolver::class);
```

The `ResponseServiceProvider` checks if `AbilitiesResolver` is bound and passes it to `ApiResponse` automatically.

API Reference
-------------

[](#api-reference)

### Success Methods

[](#success-methods)

MethodHTTP StatusReturn Type`success($data, $message, $meta)`200`JsonResponse``created($data, $message, $meta)`201`JsonResponse``updated($data, $message, $meta)`200`JsonResponse``accepted($data, $message, $meta)`202`JsonResponse``noContent()`204`JsonResponse``paginator($paginator, $resourceClass, $meta)`200`JsonResponse`### Error Methods

[](#error-methods)

MethodHTTP StatusReturn Type`fail($code, $message, $data, $meta)`configurable`void` (throws)`error($code, $message, $data, $meta)`configurable`void` (throws)`badRequest($message, $data)`400`void` (throws)`unauthorized($message)`401`void` (throws)`forbidden($message)`403`void` (throws)`notFound($message)`404`void` (throws)`unprocessable($message, $data)`422`void` (throws)Requirements
------------

[](#requirements)

- PHP ^8.1
- `ayd/api-response-base` ^1.0
- `illuminate/http` ^10.0 | ^11.0 | ^12.0
- `illuminate/support` ^10.0 | ^11.0 | ^12.0

License
-------

[](#license)

MIT

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1c85593d180ab34bd56247c69debca02794f8f07b7ee0737fc897164858de6b1?d=identicon)[GeorgeKing](/maintainers/GeorgeKing)

---

Top Contributors

[![george-nbi-ad](https://avatars.githubusercontent.com/u/188939767?v=4)](https://github.com/george-nbi-ad "george-nbi-ad (4 commits)")

### Embed Badge

![Health badge](/badges/ayd-api-response-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/ayd-api-response-laravel/health.svg)](https://phpackages.com/packages/ayd-api-response-laravel)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

815320.5k3](/packages/defstudio-telegraph)[simplestats-io/laravel-client

Analytics for Laravel. Track visitors, registrations, and payments. Discover which channels actually drive revenue, not just traffic. Server-side, GDPR compliant, ad-blocker proof.

5019.3k](/packages/simplestats-io-laravel-client)[jasara/php-amzn-selling-partner-api

A fluent interface for Amazon's Selling Partner API in PHP

1348.1k1](/packages/jasara-php-amzn-selling-partner-api)

PHPackages © 2026

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