Download the PHP package dantepiazza/laravel-api-response without Composer
On this page you can find all versions of the php package dantepiazza/laravel-api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-api-response
dantepiazza/laravel-api-response
A fluent, expressive JSend-compliant JSON API response builder for Laravel 10+.
Implements the JSend specification: every response carries a
statusofsuccess,fail, orerror, making frontend handling predictable and consistent by convention.
Installation
The service provider and ApiResponse facade are auto-discovered via Laravel's package discovery. No additional setup required.
Publish config (optional)
Usage
Three ways to use the package — all share the same singleton instance and are fully interchangeable:
For a clean DI setup across all controllers, inject in the base Controller:
Response structure
The envelope follows the JSend standard. The code field mirrors the HTTP status code and is always present in the body for clients that don't inspect headers.
success — 2xx
data is always present. It will be null if nothing was provided.
message is optional for success and fail.
fail — 4xx
Client-side error. data describes what went wrong (e.g. validation errors).
error — 5xx
Server-side error. message is required by JSend. data is optional.
API Reference
2xx — success
| Method | HTTP | Notes |
|---|---|---|
success(mixed $data = null, string $message) |
200 | Standard success |
created(mixed $data = null, string $message) |
201 | Resource created |
accepted(mixed $data = null, string $message) |
202 | Async / queued |
noContent() |
204 | No body |
4xx — fail
| Method | HTTP | Default message |
|---|---|---|
badRequest(mixed $data = null, string $message) |
400 | The request parameters are incorrect. |
unauthorized(mixed $data = null, string $message) |
401 | Unauthorized |
forbidden(mixed $data = null, string $message) |
403 | Forbidden |
notFound(mixed $data = null, string $message) |
404 | Not found |
methodNotAllowed(mixed $data = null, string $message) |
405 | Method not allowed |
conflict(mixed $data = null, string $message) |
409 | Conflict |
validationError(mixed $data = null, string $message) |
422 | Validation failed |
tooManyRequests(mixed $data = null, string $message) |
429 | Too many requests |
5xx — error
| Method | HTTP | Default message |
|---|---|---|
serverError(mixed $data = null, string $message) |
500 | Internal server error |
serviceUnavailable(mixed $data = null, string $message) |
503 | Service unavailable |
Generic setter
Use when you need full control, e.g. inside a macro:
Data helpers
Merging extra data
Multiple ->data() calls merge into the same array.
Message-only override
Validation errors
Pass errors directly as $data — they land in data per the JSend spec:
Pagination
Laravel paginator (automatic)
Pass a LengthAwarePaginator directly — all fields are extracted automatically:
Manual array / Collection
If total is omitted it defaults to count($items).
Pagination output
Both cases produce the same structure:
Cookies
Configuration
After publishing the config you can rename any top-level JSON key to match an existing API contract:
Macros
Extend the class with custom shorthand methods in any service provider:
Supported data types
data(), records(), and the $data argument of all shorthand methods accept:
- Plain
array - Any
object(cast via(array)) Illuminate\Support\CollectionIlluminate\Http\Resources\Json\JsonResourceIlluminate\Contracts\Pagination\LengthAwarePaginator(inrecords()only)
Frontend — TypeScript model
The package ships ApiResponse.ts, a single TypeScript class that maps the JSend envelope into a typed object.
Safe factory (returns null instead of throwing on invalid input):
Testing
License
MIT
All versions of laravel-api-response with dependencies
illuminate/http Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0