Looking to hire Laravel developers? Try LaraJobs

laravel-api-response-handler maintained by oyedele

Description
Standardized API responses for Laravel applications
Author
Oyedele Olufemi
Last update
2026/06/16 11:58 (dev-main)
License
Links
Downloads
3

Comments
comments powered by Disqus

Laravel Response Handler

A lightweight Laravel package for standardizing API responses across your application.

Features

  • Consistent API response structure
  • Success and error responses
  • Paginated responses
  • Production-safe server error messages
  • Laravel 11 & 12 support

Installation

Install the package via Composer:

composer require femi/laravel-response-handler

Laravel package auto-discovery will automatically register the service provider.


Usage

Import the trait into your controller:

use Femi\ResponseHandler\Traits\ResponseHandler;

class RsvpController extends Controller
{
    use ResponseHandler;
}

Standard Response Format

Success Response

{
    "status": "success",
    "message": "Operation completed successfully",
    "data": {}
}

Error Response

{
    "status": "error",
    "message": "Something went wrong",
    "data": []
}

Paginated Response

{
    "status": "success",
    "message": "Records retrieved successfully",
    "data": [],
    "pagination": {
        "current_page": 1,
        "last_page": 10,
        "per_page": 15,
        "total": 150
    }
}

Success Response Example

return $this->apiResponse(
    'Rsvp section configured successfully',
    $result,
    Response::HTTP_CREATED
);

Response:

{
    "status": "success",
    "message": "Rsvp section configured successfully",
    "data": {
        ...
    }
}

Error Response Example

return $this->apiResponse(
    $this->serverErrorMessage($th->getMessage()),
    [],
    Response::HTTP_INTERNAL_SERVER_ERROR
);

Development Environment:

{
    "status": "error",
    "message": "SQLSTATE[42S02]: Base table not found",
    "data": []
}

Production Environment:

{
    "status": "error",
    "message": "An internal server error has occured",
    "data": []
}

Paginated Response Example

return $this->paginatedResponse(
    'Users retrieved successfully',
    $users,
    [
        'current_page' => $users->currentPage(),
        'last_page' => $users->lastPage(),
        'per_page' => $users->perPage(),
        'total' => $users->total(),
    ],
    Response::HTTP_OK
);

Available Methods

apiResponse()

apiResponse(
    string $message,
    array|object|null $data,
    int $code
)

Returns a standard API response.


paginatedResponse()

paginatedResponse(
    string $message,
    array|object $data,
    array $pagination,
    int $code
)

Returns a paginated API response.


serverErrorMessage()

serverErrorMessage(string $message)

Returns:

  • Actual exception message in local/development environments.
  • Generic error message in production.

Example Controller

<?php

namespace App\Http\Controllers;

use Symfony\Component\HttpFoundation\Response;
use Femi\ResponseHandler\Traits\ResponseHandler;

class UserController extends Controller
{
    use ResponseHandler;

    public function index()
    {
        $users = User::all();

        return $this->apiResponse(
            'Users retrieved successfully',
            $users,
            Response::HTTP_OK
        );
    }
}

Requirements

  • PHP 8.2+
  • Laravel 11.x or 12.x

License

The MIT License (MIT).