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

ActiveLibrary[API Development](/categories/api)

nurbekjummayev/laravel-api-response-helpers
===========================================

A Laravel package for standardized API responses

1.1(1mo ago)011↓50%1MITPHPPHP ^8.0|^8.1|^8.2|^8.3

Since Jan 10Pushed 1mo agoCompare

[ Source](https://github.com/nurbekjummayev/laravel-api-response-helpers)[ Packagist](https://packagist.org/packages/nurbekjummayev/laravel-api-response-helpers)[ Docs](https://github.com/NurbekJummayev/laravel-api-response-helpers)[ RSS](/packages/nurbekjummayev-laravel-api-response-helpers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (15)Versions (5)Used By (1)

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

[](#laravel-api-response-helpers)

A Laravel package for standardized API responses with helpful exceptions.

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

[](#installation)

Install the package via composer:

```
composer require nurbekjummayev/laravel-api-response-helpers
```

Usage
-----

[](#usage)

### Helper Functions

[](#helper-functions)

The package provides convenient helper functions for common HTTP responses:

#### Success Responses

[](#success-responses)

```
// List with pagination
public function index(Request $request)
{
    $query = Product::query();
    $data = $query->paginate($request->get('per_page', 10));

    return okWithPaginateResponse($data);
}

// Show single resource
public function show(int $id)
{
    $model = Product::findOrFail($id);

    return okResponse($model);
}

// Create resource
public function store(Request $request)
{
    $model = Product::create($request->all());

    return createdResponse($model);
}

// Update resource
public function update(Request $request, int $id)
{
    $model = Product::findOrFail($id);
    $model->update($request->all());

    return okResponse($model);
}

// Delete resource
public function destroy(int $id)
{
    $model = Product::findOrFail($id);
    $model->delete();

    return okResponse($model);
}
```

#### Error Responses

[](#error-responses)

```
// Validation error (422)
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
    return invalidData('Validation failed', ['errors' => $validator->errors()]);
}

// Not found (404)
$model = Product::find($id);
if (!$model) {
    return notFoundRequestResponse('Product not found');
}

// Unauthorized (401)
if (!auth()->check()) {
    return unauthorizedRequestResponse();
}

// Forbidden (403)
if (!auth()->user()->can('update', $model)) {
    return forbiddenRequestResponse('Access denied');
}

// Bad request (400)
if (!$request->has('required_field')) {
    return badRequestResponse('Missing required field');
}

// Server error (500)
try {
    // Some operation
} catch (\Exception $e) {
    return serverErrorResponse('Something went wrong');
}
```

#### Custom Response

[](#custom-response)

```
return apiResponse(
    msg: 'Custom message',
    data: ['key' => 'value'],
    success: true,
    httpStatus: 200,
    errorMsg: null,
    extraData: ['meta' => ['version' => '1.0']]
);
```

### Exceptions

[](#exceptions)

The package provides exception classes that automatically render as JSON responses:

```
use NurbekJummayev\ApiResponseHelper\Exceptions\NotFoundException;
use NurbekJummayev\ApiResponseHelper\Exceptions\ForbiddenException;
use NurbekJummayev\ApiResponseHelper\Exceptions\ValidationException;

// Not Found Exception
public function show(int $id)
{
    $model = Product::find($id);

    if (!$model) {
        throw new NotFoundException('Product not found', ['product_id' => $id]);
    }

    return okResponse($model);
}

// Validation Exception
public function store(Request $request)
{
    $validator = Validator::make($request->all(), $rules);

    if ($validator->fails()) {
        throw new ValidationException(
            message: 'Validation failed',
            data: ['errors' => $validator->errors()]
        );
    }

    $model = Product::create($request->all());
    return createdResponse($model);
}

// Forbidden Exception
public function update(Request $request, int $id)
{
    $model = Product::findOrFail($id);

    if (!auth()->user()->can('update', $model)) {
        throw new ForbiddenException('You cannot update this product');
    }

    $model->update($request->all());
    return okResponse($model);
}
```

#### Available Exceptions

[](#available-exceptions)

- `ApiResponseException` - Base exception class
- `BadRequestException` - 400
- `UnauthorizedException` - 401
- `ForbiddenException` - 403
- `NotFoundException` - 404
- `ValidationException` - 422
- `MethodNotAllowedException` - 405
- `TooManyRequestsException` - 429
- `ServerErrorException` - 500

### Response Format

[](#response-format)

All responses follow a consistent structure:

**Standard Response:**

```
{
  "msg": "Success message",
  "error": null,
  "success": true,
  "data": {}
}
```

**Paginated Response:**

```
{
  "msg": "OK",
  "error": null,
  "success": true,
  "data": [
    {
      "id": 1,
      "name": "Product 1"
    },
    {
      "id": 2,
      "name": "Product 2"
    }
  ],
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 5,
    "per_page": 15,
    "to": 15,
    "total": 75
  }
}
```

**With Extra Data:**

```
{
  "msg": "Success",
  "error": null,
  "success": true,
  "data": {},
  "custom_key": "custom_value"
}
```

Testing
-------

[](#testing)

```
composer test
```

Code Quality
------------

[](#code-quality)

```
# Format code
composer format

# Test coverage
composer test-coverage
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance90

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

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 ~24 days

Total

4

Last Release

50d ago

Major Versions

0.1.1 → 1.12026-03-24

### Community

Maintainers

![](https://www.gravatar.com/avatar/d436856f44def4bfd85145727d097dc579172e677fa9dc00c4d5c12ed22a7813?d=identicon)[nurbekJummayev](/maintainers/nurbekJummayev)

---

Top Contributors

[![nurbekjummayev](https://avatars.githubusercontent.com/u/82907151?v=4)](https://github.com/nurbekjummayev "nurbekjummayev (4 commits)")

---

Tags

responsejsonapilaravelhelper

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)[kennedy-osaze/laravel-api-response

Renders consistent HTTP JSON responses for API-based projects

654.5k](/packages/kennedy-osaze-laravel-api-response)[nilportugues/laravel5-json-api-dingo

Laravel5 JSONAPI and Dingo together to build APIs fast

311.5k](/packages/nilportugues-laravel5-json-api-dingo)[nilportugues/laravel5-haljson

Laravel 5 HAL+JSON API Transformer Package

151.0k](/packages/nilportugues-laravel5-haljson)[nicklaw5/larapi

A simple Laravel 5 class for handling json api responses.

111.5k](/packages/nicklaw5-larapi)

PHPackages © 2026

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