A fluent, extensible, and developer-friendly API response builder for Laravel applications.
- ✨ Fluent Interface — Chain methods to build responses easily.
- 🧩 Response Types — Supports
success,error,validation,not found, and more. - ⚙️ Customizable Messages — Override defaults or localize them.
- 🧱 Macroable Support — Extend response types dynamically.
- 🧮 Meta Data Support — Add pagination or extra info with
meta(). - 🌍 Localization Ready — Pull response messages from Laravel translations.
- 🔍 Trace Support — Optionally include exception traces for debugging.
composer require bhry98/laravel-api-responsePublish the configuration file:
php artisan vendor:publish --provider="Bhry98\LaravelApiResponse\Providers\LaravelApiResponseServiceProvider" --tag=configThis will create config/bhry98-api-response.php where you can customize default messages:
return [
'messages' => [
'success' => 'Operation successful',
'error' => 'An error occurred',
'validation' => 'Validation failed',
'not_found' => 'Resource not found',
'internal' => 'Internal server error',
],
];use Bhry98\LaravelApiResponse\Facades\BResponseSuccess;
use Bhry98\LaravelApiResponse\Facades\BResponseError;
Route::get('/{t}', function ($t) {
return match ($t) {
"success" => BResponseSuccess::make()
->message('Everything is fine!')
->data(['id' => 1])
->additions(['request_id' => uniqid()])
->toJson(),
default => BResponseError::make()
->message('Something went wrong!')
->trace(new \Exception('Example'))
->toJson(),
};
});{
"status": "success",
"message": "Everything is fine!",
"data": { "id": 1 },
"additions": { "request_id": "654adf..." },
"meta": []
}{
"status": "error",
"message": "Something went wrong!",
"trace": "Exception stack trace (optional)",
"data": [],
"additions": [],
"meta": []
}BResponseSuccess::macro('withTimestamp', function () {
return $this->additions(['timestamp' => now()]);
});
// Usage
return BResponseSuccess::make()
->withTimestamp()
->toJson();| Method | Description |
|---|---|
make($options = []) |
Initialize response |
message($message) |
Set response message |
data($data) |
Attach data payload |
additions(array $extras) |
Add custom key/value pairs |
meta($info) |
Add pagination/meta info |
trace($exception) |
Include exception trace |
localize($key) |
Use translation key for message |
toJson() |
Return Illuminate\Http\JsonResponse |
This package is open-sourced software licensed under the MIT license.
Developed with ❤️ by Bhry98