sujon-ahmed / laravel-api-response
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
Requires
- php: ^8.1
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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, or13.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.
- Fork the repository
- Create a feature branch
git checkout -b feature/my-feature
- Commit your changes
git commit -m "Add my feature"
- Push your branch
git push origin feature/my-feature
- Open a Pull Request
๐ License
This package is open-sourced software licensed under the MIT license.
๐จโ๐ป Author
Sujon Ahmed
Full Stack Software Developer
- GitHub: https://github.com/Sujon-Ahmed
- LinkedIn: https://linkedin.com/in/sujonahmedriman/
โญ Support
If this package helps you build cleaner Laravel APIs, consider giving the repository a โญ on GitHub.
Made with โค๏ธ for the Laravel community.