Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Inertia::deepMerge Method for Handling Complex Data Merges in Responses #679

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from

Conversation

HichemTab-tech
Copy link

@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.
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.

1 participant