ho3inpgmer / laravel-api-wrapper
A Laravel package for managing API responses with a fluent interface
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/ho3inpgmer/laravel-api-wrapper
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/contracts: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
This package is not auto-updated.
Last update: 2026-01-03 12:10:01 UTC
README
A Laravel package for managing API responses with a fluent, elegant interface.
Installation
Install via Composer:
composer require ho3inpgmer/laravel-api-wrapper
Configuration
Publish the configuration file (optional):
php artisan vendor:publish --tag=api-wrapper-config
Usage
Success Response
use Ho3inpgmer\ApiWrapper\ApiResponse; // Basic success response return ApiResponse::success(['user' => $user]); // With custom message return ApiResponse::success($data, 'User created successfully', 201); // Without data return ApiResponse::success()->withoutData(); // Add extra fields return ApiResponse::success($data) ->add('meta', ['page' => 1]) ->add('links', ['next' => 'url']);
Error Response
// Basic error response return ApiResponse::error(['email' => 'Invalid email']); // With custom status and message return ApiResponse::error($errors, 404, 'Resource not found'); // Without errors field return ApiResponse::error()->withoutErrors();
Method Chaining
return ApiResponse::success($data) ->withStatus(201) ->add('meta', $meta) ->withoutMessage();
Response Format
Success response:
{
"ok": true,
"message": "عملیات با موفقیت انجام شد",
"data": {}
}
Error response:
{
"ok": false,
"message": "خطا در انجام عملیات",
"errors": []
}
Available Methods
success($data, $message, $status)- Create a success responseerror($errors, $status, $message)- Create an error responsewithoutMessage()- Remove message from responsewithoutData()- Remove data from responsewithoutErrors()- Remove errors from responsewithStatus($status)- Set custom HTTP status codeadd($key, $value)- Add extra fields to response
Testing
composer test
License
MIT License. Please see License File for more information.
Credits
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.