Skip to content

Conversation

@HichemTab-tech
Copy link
Contributor

@HichemTab-tech HichemTab-tech commented Oct 26, 2024

This PR adds the Inertia::deepMerge method to the Laravel package, complementing the new deepMerge feature 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 the Inertia response. Unlike the existing merge method, deepMerge performs recursive merging to handle arrays and nested properties effectively.

Usage Example:

return Inertia::render('Dashboard', [
    'items' => Inertia::deepMerge(Item::paginate())
]);

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 data array in a pagination object but leave meta and links intact or only partially updated:

'items' => [
    'data' => [...],   // Appending new data
    'meta' => [...],   // Meta data remains consistent
    'links' => [...],  // Links updated if needed
]

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:

Introduce a deepMerge method to handle nested property merges more effectively. This addition includes updates to the Response, MergesProps, and ResponseFactory classes, enabling nested properties to be merged and tracked separately.
Updated the merge logic to exclude properties that require deep merging, ensuring only shallow merges are considered. This fix prevents unintended deep merges, improving the accuracy of the merge process.
Implemented deep merging for response props and adjusted the existing merge logic to accommodate this feature. Added unit tests to ensure the deep merge functionality works correctly with various scenarios.
Simplify the merging logic by using a conditional check before assigning properties. This makes the code shorter and easier to understand, while maintaining the same functionality.
@joetannenbaum
Copy link
Contributor

Thanks @HichemTab-tech!

@joetannenbaum joetannenbaum merged commit d584a2f into inertiajs:2.x Apr 9, 2025
@HichemTab-tech HichemTab-tech deleted the feature/add-deep-merge-props branch May 29, 2025 12:56
adrum added a commit to adrum/InertiaCore that referenced this pull request Sep 22, 2025
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.

2 participants