Having built a couple of moderately complex API's - both in Laravel - I appreciate the value of transforming data before sending it in a response.
What I am struggling with is how this data should be handled when it comes back, so for create and especially update methods. Should it be transformed (but in reverse)?
For example, say we have the following Task transformer:
class ContractTransformer extends Transformer
{
/**
* @param $task
* @return array
*/
public function transform($task)
{
return [
'id' => $task['id'],
'title' => $task['title'],
'description' => $task['description'],
'due_date' => $task['end_date']
];
}
}
Note that the value for the column 'end_date' in the database is returned as 'due_date' - a primitive example of returning the column name in a more user friendly way.
Say we have a user consuming this API and building the front end with Angular/React or another framework, they receive a value for 'due_date' and therefore surely they would expect to send it back (for an update operation) as 'due_date' rather than 'end_date'?
Then we have the problem (especially for large objects with many fields) of having to do something like the following:
$task = Task::find($id);
$task->title = $request->input('title');
$task->description = $request->input('description');
// and so on...
$task->save();
Rather than using:
$request->all();
And it means if column names change we have to update all the update methods too.
Is there a good way to handle this? Should the data be 're-transformed'?