- How do I install and use this package in my Laravel 12/13 project?
- Run `composer require f9webltd/laravel-api-response-helpers`, then add the `ApiResponseHelpers` trait to your controller or base controller. Call methods like `respondWithSuccess($data)` or `respondError($message)` to replace manual `response()->json()` calls.
- Does this package work with Laravel API Resources (JsonResource)?
- Yes, the package supports Laravel API Resources out of the box. You can pass a `JsonResource` instance directly to methods like `respondWithSuccess()`, and it will be automatically converted to JSON.
- Can I customize the default success/error response structure?
- Yes, the package provides a `setDefaultSuccessResponse()` method to override default keys like `data`, `message`, or `status`. You can also extend the trait to add custom logic for specific response formats.
- Will this break existing API endpoints if I adopt it incrementally?
- No, you can migrate endpoints gradually. The package doesn’t enforce usage—you can keep existing `response()->json()` calls while adopting the trait for new or refactored endpoints.
- Does it support HTTP status codes beyond 200/404 (e.g., 422 for validation errors)?
- Yes, the package includes methods like `respondWithError($message, 422)` to specify custom status codes. You can also create your own helper methods for specific use cases.
- How does this handle pagination in API responses?
- The package doesn’t enforce pagination standards, but you can pass paginated collections (e.g., from `paginate()`) to `respondWithSuccess()`, and it will serialize them normally. For custom pagination metadata, extend the trait or use middleware.
- Is there a way to add custom headers or metadata to responses?
- Yes, you can modify the trait to include additional logic, such as adding headers via `response()->header()` or appending metadata to the response array before returning it.
- Does this package work with Laravel’s built-in validation error responses?
- No, it doesn’t replace Laravel’s validation error formatting. However, you can use `respondWithError()` for custom error messages while keeping validation errors as-is or extending the trait to merge them.
- What Laravel versions and PHP versions are officially supported?
- The package supports Laravel 11–13 and PHP 8.2+. Check the [README](https://github.com/f9webltd/laravel-api-response-helpers) for legacy support details, but it’s recommended to use actively supported versions.
- How do I test API responses generated by this package?
- Test responses like any other JSON output in Laravel. Use assertions to verify status codes, data structure, and keys (e.g., `assertJsonStructure(['data', 'message'])` in Pest or PHPUnit). The package’s methods return standard `JsonResponse` objects.