Skip to content

[12.x] Add data_map helper #56007

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

Closed
wants to merge 1 commit into from
Closed

[12.x] Add data_map helper #56007

wants to merge 1 commit into from

Conversation

Seb33300
Copy link

This PR adds a new helper to the Collection helper. In addition to the existing data_set which is limited to set static data, this PR adds the data_map helper that can be used to map a callable using dot notation.

For example:

$data = [
    'company' => [
        'name' => 'Acme Corporation',
        'employees' => [
            ['name' => 'John Doe', 'age' => 30],
            ['name' => 'Jane Smith', 'age' => 25],
        ]
    ]
];

data_map($data, 'company.employees.*.age', fn ($old) => ++$old);

// Result
$data = [
    'company' => [
        'name' => 'Acme Corporation',
        'employees' => [
            ['name' => 'John Doe', 'age' => 31],
            ['name' => 'Jane Smith', 'age' => 26],
        ]
    ]
];

The callable can accept 2 params:

  • $old : previous value or null if not defined
  • $key : the key to the mapped item (eg: company.employees.0)

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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