Transform Keys
#53747
Replies: 3 comments 2 replies
-
Hi. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can, however, already kinda do this: $coll = collect([ 'a' => 1, 'b' => 4 ]);
$coll->mapWithKeys(fn($value, $key) => [ match($key) {
'a' => 'c',
'b' => 'z',
} => $value ]);
/*
Results in:
[
'c' => 1,
'z' => 4,
]
*/ It might look ugly at first but actually it's just your mapping array expressed as a match expression. |
Beta Was this translation helpful? Give feedback.
2 replies
-
See also this https://github.com/jarektkaczyk/eloquence-mappable |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Have a built-in method for transforming/replacing keys, both in collections and in validation rules.
Validation
I'd love to be able to transform the incoming keys in the request using a map or definition that transforms the keys as I need them to.
Example:
Illuminate\Support\Collection
Perhaps, less important (to me at least) is a built-in to transform the keys of a collection based on a supplied key map. So, for example:
Reasoning
I have an SPA that has gone through several teams and developers and each of them have introduced their own "style" into my API. THe problem is that:
The result is an API that looks like dog turd (in terms of standards).
We are refactoring the UI and API slowly over time, but it would be amazing if we could refactor small parts of the API at a time without introducing big changes with a ton of boilerplate code to make this work.
In general, this would lighten the changes necessary because we could modify the request to accept both a "starts" and "starts_at" input (which means the UI wouldn't break).
The API wouldn't care which one you supplied (it would simply respect that "starts_at" takes precedence over "starts" if they're both supplied) and the -$request->validated() would return the snake_case keyed array for easy use with the Model->fill() methods.
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions