Skip to content

Conversation

achrafAa
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants