Looking to hire Laravel developers? Try LaraJobs

laravel-api-wrapper maintained by ho3inpgmer

Description
A Laravel package for managing API responses with a fluent interface
Author
Last update
2025/10/24 15:10 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

Laravel API Wrapper

A Laravel package for managing API responses with a fluent, elegant interface.

Latest Version on Packagist Total Downloads

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 response
  • error($errors, $status, $message) - Create an error response
  • withoutMessage() - Remove message from response
  • withoutData() - Remove data from response
  • withoutErrors() - Remove errors from response
  • withStatus($status) - Set custom HTTP status code
  • add($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.