Skip to content

[12.x] Adds simple key transformation to Collection and Arr helper. - #57352

Closed
DarkGhostHunter wants to merge 1 commit into
laravel:12.xfrom
DarkGhostHunter:feat/12.x/transform-keys
Closed

DarkGhostHunter wants to merge 1 commit into
laravel:12.xfrom
DarkGhostHunter:feat/12.x/transform-keys

Conversation

@DarkGhostHunter

@DarkGhostHunter DarkGhostHunter commented Oct 11, 2025

Copy link
Copy Markdown
Contributor

What?

Introduces a method to both base Collection and Arr helpers to transform each array key using a callback.

The main idea for this is to avoid extensive callbacks or using mapWithKeys() just to change the keys when these come from an endpoint that uses camelCase or kebab-case for its keys (since the convention on Laravel is to use snake_case)

The Arr::mapKeys() will return a new array with the new keys.

use Illuminate\Support\Arr;

$array = [
    'first_name' => 'John',
    'is_admin' => false
];

$new = Arr::mapKeys($array, fn ($key) => Str::replace('_', '-'));

// [
//     'first-name' => 'John',
//     'is-admin' => false,
// ]

The Collection::mapKeys() will also do the same.

use Illuminate\Support\Collection;use Illuminate\Support\Str;

$collection = Collection::make([
    'first_name' => 'John',
    'is_admin' => false
]);

$collection->mapKeys(fn ($key) => Str::replace('_', '-'));

// \Illuminate\Support\Collection([
//     'first-name' => 'John',
//     'is-admin' => false,
// ])

The main idea for this is to avoid extensive callbacks or using mapWithKeys() just to change the keys. Personally I like to use this to allow the developer to issue either an array of items or multiple parameters, like when these come from parsed JSON into an array without sort warranties, but also allow to translate each key for frontend reasons.

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

public function make(array|string $name, string $streetAddress = '', string $cityName = '')
{
    if (is_array($name)) {
        return $this->make(...Arr::mapKeys($name, Str::camel(...)))
    }
    
    // ...
}

With that, this PR also introduces mapKeysToCamel and mapKeysToSnake into the Arr helper to conveniently change the case of the array keys, which should be useful when using JSON values as parameters like the above example, or exposing an object as JSON when being serialized to an array.

use Illuminate\Support\Arr;

public function make(array|string $name, string $streetAddress = '', string $cityName = '')
{
    if (is_array($name)) {
        return $this->make(...Arr::mapKeysToCamel($name))
    }
    
    // ...
}

public function toArray(): array
{
    return Arr::mapKeysToSnake(get_object_vars($this));
}

@shaedrich

Copy link
Copy Markdown
Contributor

With that, this PR also introduces mapKeysToCamel and mapKeysToSnake into the Arr helper to conveniently change the case of the array keys, which should be useful when using JSON values as parameters like the above example, or exposing an object as JSON when being serialized to an array.

What about mapKeysToLower, mapKeysToUpper, mapKeysToKebab, mapKeysToConstantCase, and mapKeysToPascal?

@DarkGhostHunter

Copy link
Copy Markdown
Contributor Author

If this PR gets accepted, may be, but this is intended to make easier transforming array keys for JSON convention to/from property name convention

@shaedrich

Copy link
Copy Markdown
Contributor

This sounds like the PR is tailored to your needs not to a general use case

@DarkGhostHunter
DarkGhostHunter marked this pull request as draft October 13, 2025 00:15
@DarkGhostHunter
DarkGhostHunter force-pushed the feat/12.x/transform-keys branch 3 times, most recently from de81dee to bf8b73b Compare January 25, 2026 20:15
@DarkGhostHunter
DarkGhostHunter marked this pull request as ready for review February 5, 2026 15:06
@DarkGhostHunter
DarkGhostHunter marked this pull request as draft February 5, 2026 15:11
@DarkGhostHunter
DarkGhostHunter force-pushed the feat/12.x/transform-keys branch from bf8b73b to 829be43 Compare February 5, 2026 15:26
@DarkGhostHunter
DarkGhostHunter marked this pull request as ready for review February 5, 2026 15:26
@DarkGhostHunter
DarkGhostHunter marked this pull request as draft February 5, 2026 19:36
@DarkGhostHunter
DarkGhostHunter deleted the feat/12.x/transform-keys branch February 5, 2026 22:33
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