Search by

sujon-ahmed / laravel-api-response

Sujon-Ahmed

A simple and reusable API response package for Laravel.

Package info

github.com/Sujon-Ahmed/laravel-api-response

pkg:composer/sujon-ahmed/laravel-api-response

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-09-09 18:49 UTC

This package is auto-updated.

Last update: 2026-09-09 19:21:41 UTC


README

Latest Version Total Downloads PHP Version License

A simple, clean, and reusable API response package for Laravel applications.

Standardize your API responses with a consistent structure for successful and failed requests.

โœจ Features

  • โœ… Simple success() response
  • โŒ Simple error() response
  • ๐Ÿ“ฆ Reusable across Laravel projects
  • ๐Ÿš€ Laravel auto-discovery support
  • ๐Ÿ”Œ Facade support
  • ๐ŸŽฏ Custom HTTP status codes
  • ๐Ÿงฉ Clean and lightweight
  • ๐Ÿ”’ No unnecessary dependencies

๐Ÿ“‹ Requirements

  • PHP ^8.1
  • Laravel 10.x, 11.x, 12.x, or 13.x

๐Ÿ“ฆ Installation

Install the package using Composer:

composer require sujon-ahmed/laravel-api-response

The package supports Laravel's package auto-discovery, so no manual service provider registration is required.

๐Ÿš€ Usage

Success Response

Use the ApiResponse facade to return a successful API response:

use SujonAhmed\ApiResponse\Facades\ApiResponse;

return ApiResponse::success(
    data: [
        'id' => 1,
        'name' => 'Sujon Ahmed',
    ],
    message: 'User fetched successfully'
);

Response:

{
  "success": true,
  "message": "User fetched successfully",
  "data": {
    "id": 1,
    "name": "Sujon Ahmed"
  }
}

The default HTTP status code is:

200 OK

Error Response

Return an error response using:

use SujonAhmed\ApiResponse\Facades\ApiResponse;

return ApiResponse::error(
    message: 'User not found',
    status: 404
);

Response:

{
  "success": false,
  "message": "User not found",
  "data": null
}

HTTP status:

404 Not Found

Custom Status Code

You can provide any valid HTTP status code:

return ApiResponse::success(
    data: $user,
    message: 'User created successfully',
    status: 201
);

Response:

{
  "success": true,
  "message": "User created successfully",
  "data": {
    "id": 1,
    "name": "Sujon Ahmed"
  }
}

HTTP status:

201 Created

๐Ÿงฑ API Response Structure

Success

{
  "success": true,
  "message": "Success",
  "data": {}
}

Error

{
  "success": false,
  "message": "Something went wrong",
  "data": null
}

This consistent structure makes API responses easier to consume from:

  • React
  • Next.js
  • Vue
  • Mobile applications
  • Third-party integrations

๐ŸŽฏ Controller Example

<?php

namespace App\Http\Controllers;

use App\Models\User;
use SujonAhmed\ApiResponse\Facades\ApiResponse;

class UserController extends Controller
{
    public function show(int $id)
    {
        $user = User::find($id);

        if (!$user) {
            return ApiResponse::error(
                message: 'User not found',
                status: 404
            );
        }

        return ApiResponse::success(
            data: $user,
            message: 'User fetched successfully'
        );
    }
}

๐Ÿ”„ Typical API Flow

Request
   โ†“
Controller
   โ†“
Business Logic
   โ†“
ApiResponse
   โ†“
JSON Response

Example:

GET /api/users/1
        โ†“
   UserController
        โ†“
   Find User
        โ†“
 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ†“             โ†“
Found        Not Found
 โ†“             โ†“
success()    error()
 โ†“             โ†“
200 OK       404

๐Ÿงช Testing

Clone the repository:

git clone https://github.com/Sujon-Ahmed/laravel-api-response.git

Install dependencies:

composer install

Run the test suite:

composer test

Or, if your project uses Laravel's test runner:

php artisan test

๐Ÿค Contributing

Contributions, issues, and feature requests are welcome.

  1. Fork the repository
  2. Create a feature branch
git checkout -b feature/my-feature
  1. Commit your changes
git commit -m "Add my feature"
  1. Push your branch
git push origin feature/my-feature
  1. Open a Pull Request

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ‘จโ€๐Ÿ’ป Author

Sujon Ahmed

Full Stack Software Developer

โญ Support

If this package helps you build cleaner Laravel APIs, consider giving the repository a โญ on GitHub.

Made with โค๏ธ for the Laravel community.