Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Api Response Helpers Laravel Package

f9webltd/laravel-api-response-helpers

Laravel package providing a simple trait to generate consistent JSON API responses across your app. Works with PHP 8.2+ and Laravel 11–13, offering helper methods for common success and error response patterns.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides standardized helper methods for consistent API response formatting in Laravel applications. After installing via Composer (composer require f9webltd/laravel-api-response-helpers), extend your base API controller with F9Web\ApiResponseHelpers\ApiResponseTrait. This exposes core methods like success(), error(), notFound(), and validationError(), along with new HTTP-specific helpers: respondAccepted(), respondConflict(), and respondTooManyRequests().

For first-time use, replace manual response()->json() calls with:

// Standard responses
$this->success(['data' => $model]);
$this->error('Failed', 400);

// New HTTP-specific responses
$this->respondAccepted(); // 202 Accepted
$this->respondConflict('Duplicate entry'); // 409 Conflict
$this->respondTooManyRequests(); // 403 Too Many Requests

Implementation Patterns

  • Core Workflow: Use trait methods in all API controllers to enforce consistent response structure (e.g., { "status": "success", "data": ..., "message": ... }).
  • HTTP-Specific Responses: Leverage new methods for idempotent operations (respondAccepted()), conflict handling (respondConflict()), and rate-limiting (respondTooManyRequests()).
  • Config Customization: Publish the config (php artisan vendor:publish --tag="api-response-helpers-config") to adjust status keys, messages, or HTTP codes globally.
  • Validation/Exceptions: Return validation failures via $this->validationError($validator) or wrap Model::firstOrFail() with $this->notFound('Resource not found').
  • Deprecation Note: Replace respondNoContent() calls with response()->noContent() (RFC 9110 compliance) or await the next major version for a zero-arg wrapper.

Gotchas and Tips

  • Breaking Change: v3.0+ requires Laravel ≥11 and PHP ≥8.2. Verify compatibility before upgrading.
  • Trait Method Safety: Methods are type-agnostic—ensure $data passed to success() is serializable (avoid closures/resources).
  • Exception Handling: The trait does not intercept exceptions. Use Laravel’s render() or global reportable callbacks for centralized error logging.
  • Deprecation Warning: respondNoContent() is tagged @deprecated—update code to use response()->noContent() or plan for the next major release’s zero-arg wrapper.
  • Config Dependency: Customize via config/api-response-helpers.php to align with front-end expectations (e.g., snake_case vs camelCase fields).
  • Method Naming: Avoid naming collisions in controllers by not declaring methods with the same names as the trait’s helpers.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle