- What is shipsaas/laravel-resource-reducer used for in Laravel?
- It is an optional payload-shaping utility for Laravel API responses. It trims fields, relationships, and nested resources so endpoints return only what a client requests instead of a big JSON payload. It works at the JsonResource presentation layer and is not a replacement for resource classes, serialization contracts, or authorization.
- How do I install laravel-resource-reducer?
- Install it with Composer in a Laravel application and use v1.1.1 as the first reliable baseline because that patch corrects missing composer.json configuration. After install, verify package discovery and autoloading in a clean Laravel skeleton before wiring it into an endpoint. Confirm the package Laravel and PHP version constraints first.
- Which Laravel and PHP versions does laravel-resource-reducer support?
- The provided context does not list explicit Laravel or PHP version constraints, so check the package composer.json before adding it to an application. Treat v1.1.1 as the baseline and test a clean Composer install on your target Laravel version. Do not assume compatibility with older or newer framework releases without verification.
- What is the allowlist or denylist syntax for selecting fields?
- The exact fluent API and allowlist or denylist syntax are not specified in the available context, so review the package README and source before implementation. The package is described as using lean, reusable resource classes to trim fields, relationships, and nested resources. Document your reducible-field policy and cover it with response-shape tests.
- Does laravel-resource-reducer reduce database queries or prevent N+1 problems?
- It is positioned as a response-shaping helper, so do not assume it reduces database load automatically. You need to confirm whether dropped relationships prevent eager loading or only hide data that was already loaded. Reducing JSON size can improve bandwidth and client rendering, but query optimization still belongs in your resource and query layer.
- Can per-request field selection break my API contract?
- Yes, per-request field selection can cause contract drift, inconsistent client behavior, and cache-key complexity if defaults are not explicit. Version client-facing schemas and document which fields are reducible before enabling it. Add response-shape contract tests and pilot on one non-critical read-only endpoint behind a feature flag.
- Does laravel-resource-reducer replace Laravel API Resources or authorization?
- No. It fits alongside existing JsonResource classes as a presentation helper that trims output, and it should not replace serialization contracts or authorization logic. Field selection must be safe by default, and authorization still needs to be enforced in your application.
- Is laravel-resource-reducer safe to use in production?
- Direct runtime risk is low if you apply it narrowly to response shaping, but product risk is medium due to low adoption signals such as 46 stars and the last release on 2024-03-13. Pin the version, track upstream issues, and avoid making it a critical dependency for core API contracts. Require internal documentation for supported versions, configuration, reducible fields, and rollback.
- How should I migrate an existing Laravel API to resource reduction?
- Start with a short spike in a disposable Laravel app, inventory target JsonResource classes, and classify fields as safe-to-trim versus contract-critical. Update to v1.1.1, refresh the lockfile, clear config and package caches, and verify package discovery. Baseline existing API contracts, add response-shape tests, pilot one endpoint behind a feature flag, then expand endpoint by endpoint.
- How does field selection interact with API response caching?
- Cache keys must include the requested field set, or cached responses may be reused incorrectly across clients that asked for different fields. Because reduction changes response shape per request, caching needs to account for the selected reducible fields. Monitor payload size, error rates, and client compatibility after rollout.