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

[12.x] mergeIfMissing allows merging with nested arrays #52242

Merged
merged 3 commits into from
Jul 29, 2024

Conversation

KIKOmanasijev
Copy link
Contributor

@KIKOmanasijev KIKOmanasijev commented Jul 23, 2024

Hello,

This PR updates the mergeIfMissing method from the Http/Request class to allow merging with nested arrays using the "." (dot) notation.

The section named "Actual Behaviour" marks Laravel's current implementation output for the described scenario, while the "Expected Behaviour" shows the output if using my changes to the Http/Request class.

I've added 2 assertions to the testMergeIfMissingMethod test.

Problem / Setting up the scenario:

  • I've been sending POST requests to my endpoint with the following payload:
{
    "user": {
        "first_name: "Taylor"
    }
}

and tried merging the input in my controller with:

$request->mergeIfMissing([
    'user.last_name' => 'Otwell'
]);

Expected Behaviour

expecting to set the current request input to:

[
    'user' => [
        'first_name' => 'Taylor',
        'last_name' => 'Otwell
    ]
]

Actual Behaviour

but instead I was getting:

{
    "user": {
        "first_name": "Taylor"
    },
    "user.last_name": "Otwell"
}

@taylorotwell taylorotwell marked this pull request as draft July 23, 2024 19:18
@KIKOmanasijev KIKOmanasijev marked this pull request as ready for review July 23, 2024 19:46
@KIKOmanasijev
Copy link
Contributor Author

Hey @driesvints I've fixed the failing tests from this PR. Can you take a look at this whenever you have the time, and let me know if you need anything from my side?

@driesvints driesvints changed the title feat: mergeIfMissing allows merging with nested arrays [12.x] mergeIfMissing allows merging with nested arrays Jul 26, 2024
@taylorotwell taylorotwell merged commit 42be005 into laravel:master Jul 29, 2024
28 checks passed
@matiazar
Copy link

@KIKOmanasijev thank you !!!

@eldair
Copy link
Contributor

eldair commented Feb 25, 2025

@KIKOmanasijev am I missing something or does this affect merge method as well since all the changes are on that method?

@KIKOmanasijev
Copy link
Contributor Author

KIKOmanasijev commented Feb 25, 2025

@eldair Nope, you are not missing out on anything. mergeIfMissing was relying on merge to handle the merging of the request data.

Basically, merge() gets the upgrade, and mergeIfMissing() benefits from it without needing its own separate tweak.

I guess I could have been more concise when choosing the PR title.

@eldair
Copy link
Contributor

eldair commented Feb 25, 2025

Thanks for clarifying. My concern is the upgrade guide for laravel 12, which explicitly mentions mergeIfMissing only, while the real change is much bigger and will affect more people.

Otherwise I'm more than happy that this is for the merge method as well.

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.

4 participants