skip to content
TJ Miller

Abstracting Request Keys

/ 1 min read

In an API’s codebase, it’s a good idea to abstract the keys for the requests and responses on top of your data layer. This is a key step to making it easier for your codebase to evolve and still uphold your API contract.

In Laravel we have Resource Responses to help abstract our model data for responses. For request data, I wanted a lightweight key mapping to help add a little abstraction layer.

First I created a little Request macro:

Request::macro('mapKeys', function ($keyMap) {
->each(function ($attribute, $key) use ($keyMap) {
if (array_key_exists($key, $keyMap)) {
$this->json->set($keyMap[$key], $attribute);
return $this;

Now you can use it in your controllers to map from request data to domain data.

public function store()
return AccountResource::make(Account::create(request()
'gateway_username' => 'username',
'gateway_password' => 'password'
'username' => 'required',
'password' => 'required',
'environment' => 'required|in:production,sandbox',

One thing I’ll still need to figure out is mutating nested keys, but I’ll cross that bridge when I get there!


For more thoughts, links, project and personal updates.