sardar-backend / lara-response
There is no license information available for the latest version (dev-master) of this package.
A Laravel package for API response handling
dev-master
2025-04-10 17:59 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
This package is auto-updated.
Last update: 2025-09-10 19:05:12 UTC
README
A robust and customizable API response helper for Laravel applications, designed to simplify JSON responses and maintain consistency across your API.
📌 Features
- Standardized API response structure
- Effortless HTTP status code management
- Append additional metadata seamlessly
- Fluent and intuitive interface
- Fully compatible with Laravel's response system
📦 Installation
Install the package via Composer:
composer require sardar-backend/lara-response:dev-master
🚀 Usage
1️⃣ Generate the Service Class
First, create a new service class using the following command:
php artisan make:service ModelName
To properly inject ProductServices
into your controller, use the constructor method:
public function __construct(private ProductServices $productServices) { }
Basic Example
use SardarBackend\RestfulApiHelper\RestfulApi\Fecades\ApiResponseFacade; $result = $this->productServices->getAll($request->all()); if (!$result->ok) { return ApiResponseFacade::withMessage($result->data)->withStatus(500)->build()->Response(); } return ApiResponseFacade::withData(ApiProductListResource::collection($result->data)->resource)->build()->Response();
Adding Additional Data
use SardarBackend\RestfulApiHelper\RestfulApi\Fecades\ApiResponseFacade; $result = $this->productServices->getAll($request->all()); if (!$result->ok) { return ApiResponseFacade::withMessage($result->data)->withStatus(500)->build()->Response(); } return ApiResponseFacade::withData(ApiProductListResource::collection($result->data)->resource)->withAppends(['processing_time' => '2 seconds'])->build()->Response();
🛠 Methods
Method | Description |
---|---|
withMessage(string $message) |
Sets the response message |
withData(mixed $data) |
Sets the response data |
withStatus(int $status) |
Sets the HTTP status code |
withAppends(array $appends) |
Adds additional key-value pairs to the response |
response() |
Returns a Laravel JsonResponse object |
📜 License
This package is open-source and licensed under the MIT License.
🤝 Contributing
Contributions are welcome! Feel free to submit issues or pull requests on GitHub.
📬 Contact
For any inquiries, reach out via GitHub Issues.