Add Inertia::deepMerge Method for Handling Complex Data Merges in Responses #679
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.
This PR adds the
Inertia::deepMergemethod to the Laravel package, complementing the newdeepMergefeature introduced in Inertia.js (see the corresponding PR in the Inertia.js repository). This provides developers with the ability to merge deeply nested data structures in their props, such as arrays and objects within objects, without completely replacing the existing properties.New Method:
Inertia::deepMerge(): This method will mark specific props for deep merging when used with theInertiaresponse. Unlike the existingmergemethod,deepMergeperforms recursive merging to handle arrays and nested properties effectively.Usage Example:
Why This Is Useful:
When dealing with complex data, such as a paginated dataset, it’s often necessary to update only parts of the data rather than replacing the entire object. For example, suppose we want to append items to the
dataarray in a pagination object but leavemetaandlinksintact or only partially updated:The
Inertia::deepMerge()method ensures that nested properties are correctly merged instead of overwritten, offering a more granular approach to updating props in dynamic, real-time applications.Related PR:
This PR works in conjunction with the newly added deep merging logic in the Inertia.js repository. Please see Inertia.js PR #2069 for details on the client-side implementation.
Related Issue: