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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add data_remove helper #47618

Merged
merged 3 commits into from Jul 8, 2023

Conversation

PhiloNL
Copy link
Contributor

@PhiloNL PhiloNL commented Jun 30, 2023

This PR adds a new helper to the Collection helper. In addition to the existing data_get, data_set, and data_fill, this will add the data_remove helper to remove keys from objects or arrays easily. This is ideal if you want to remove keys from nested objects or arrays using the wildcard helper.

For example:

$data = [
    'article' => [
        'title' => 'Foo',
        'comments' => [
            ['comment' => 'foo','name' => 'First'],
            ['comment' => 'bar','name' => 'Second'],
        ]
    ]
];

data_remove($data, 'article.comments.*.name');

// Result
$data = [
    'article' => [
        'title' => 'Foo',
        'comments' => [
            ['comment' => 'foo'],
            ['comment' => 'bar'],
        ]
    ]
];

I wasn't sure about naming the function data_forget instead of data_remove as both terms are used throughout the framework. Let me know if you want the name changed.

Have a great day! 馃檶馃徎

@milwad-dev
Copy link
Contributor

I think you can use Collection!

@PhiloNL
Copy link
Contributor Author

PhiloNL commented Jun 30, 2023

I think you can use Collection!

@milwad-dev can you please elaborate? The Illuminate\Support\Collection@forget method does not allow the usage of the dot notation or wildcards but I'm not sure if this is what you referring to.

@devajmeireles
Copy link
Contributor

As per the fact that we have data_get, data_set and data_fill I agree with that. I think it will be very useful. Thanks.

Just a question: following the naming convention of existing helpers, why not data_unset instead of data_remove?

@PhiloNL
Copy link
Contributor Author

PhiloNL commented Jun 30, 2023

@ajmeireles as mentioned in the PR, I wasn't sure about naming the function data_forget instead of data_remove as both terms are used throughout the framework. data_unset is also an option of course but I'll let the Laravel team decide :)

@milwad-dev
Copy link
Contributor

@PhiloNL
You're right, great!

@taylorotwell taylorotwell merged commit f7adb61 into laravel:10.x Jul 8, 2023
16 checks passed
@PhiloNL PhiloNL deleted the feature/add-data-remove-helper branch July 8, 2023 21:52
@oyepez003
Copy link

@huangdijia
Copy link
Contributor

Why don't use just https://laravel.com/docs/10.x/helpers#method-array-forget

Arr::forget is limited to arrays, while data_forget can manipulate objects.

@cdinopol
Copy link

Had a package similar to this from long time ago: https://github.com/cdinopol/data-guard

@PhiloNL
Copy link
Contributor Author

PhiloNL commented Jul 11, 2023

Why don't use just https://laravel.com/docs/10.x/helpers#method-array-forget

Arr::forget also doesn't support wildcards.

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.

None yet

7 participants