add $forcewrapping documentation #10778
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Docs: Add $forceWrapping property usage to resource data wrapping section
Summary
This PR updates the Eloquent API Resources - Data Wrapping documentation to include details about the new $forceWrapping static property introduced in laravel/framework#56736.
Motivation
Laravel's resource response wrapping can behave inconsistently when the resource's toArray() output includes a top-level key that matches the wrapper (typically data). In such cases, Laravel skips wrapping to avoid nesting the data key twice. While this behavior is intentional, it leads to unpredictable API responses based on the model's attributes.
To resolve this, Laravel now supports an opt-in $forceWrapping property on JsonResource, allowing developers to explicitly force wrapping, even if the wrapper key already exists in the resource data.
Changes
Adds a new subsection: Force Wrapping under the existing #data-wrapping heading.
Documents how and why to use the $forceWrapping static property.
Provides a complete usage example with before/after output to demonstrate the impact of enabling $forceWrapping.
Benefits
Helps developers write more predictable and stable API responses.
Encourages explicit behavior over implicit side effects.
Reduces bugs caused by model attributes accidentally colliding with the wrapper key.
Highlights a backward-compatible feature that aligns with Laravel’s expressive resource design.