Our Eloquent models have attributes following the Laravel snake case convention.
e.g. first_name, last_name and created_at
Although my frontend (react) follows the javascript camel case standard.
e.g. firstName, lastName and createdAt
Is there a simple way to convert ALL attributes to camel case when sending an API response?
We are using Larave 5.2 (and the Dingo API package of course)
In your transformer, in method transform there will be a toArray call on your model. You could change those keys in transform method, or you could add on your BaseModel a
public function toArrayCamel()
{
$array = $this->toArray();
foreach($array as $key => $value)
{
$return[camel_case($key)] = $value;
}
return $return;
}
and use it in your transformer
Aye, use a transformer,
yeah, we are using a customer transformer. Although now we face the problem of transforming all incoming requests back to snake_case... face slap
Why is it such a struggle to get laravel to use a different case standard?
We implemented a middleware the transforms everything fine, although the problem is for some strange reason FormRequests do not have HTTP middleware stack applied to them...
Why do you need to send incoming keys as camel case?
Dealing with camelCase everywhere e.g. JavaScript, backend etc... And having snake_case database tables is really frustrating
fields should be snake_case in your database and php. In you JS you could have transformers on your xhr request that transform from camelCase to snake_case on reading and writing over http so you could use in your js code withought having lint screaming about your keys
Almost any other api framework is able to accept input as the standard json camelCase . I strongly agree that this should be added here
@geocine the package doesnt care what the keys come in as, and Laravel supports any input key as well (maybe not any, I haven't tried using an emoji as an input key yet). So getting the input as myField and mapping it to my_field into the database shouldn't be difficult.
Only time that'll give you a headache is if you're just dumping $request->input() into the database insert directly, but you can just use a variety of array functions or a collection pipeline to transform the input keys from camelCase to snake_case
@hskrasek That is exactly what I did, I created my own Request implementation
I expected to find a tunning built-in feature, but I couldn't find it.
One of the solutions:
https://github.com/grohiro/laravel-camelcase-json
If anyone is still wondering how to best approach, I have an API boilerplate (for which this package is a basis for) - https://github.com/specialtactics/l5-api-boilerplate
You can have a look at the transformer, it does quite a lot of interesting things, including solving this problem -
https://github.com/specialtactics/l5-api/blob/master/src/Transformers/RestfulTransformer.php
If anyone is still wondering how to best approach, I have an API boilerplate (for which this package is a basis for) - https://github.com/specialtactics/l5-api-boilerplate
You can have a look at the transformer, it does quite a lot of interesting things, including solving this problem -
https://github.com/specialtactics/l5-api/blob/master/src/Transformers/RestfulTransformer.php
Since it's about 2 years since the last reply I'd like to add the best approach for now would be to use ApiResources
https://laravel.com/docs/7.x/eloquent-resources#writing-resources
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'teaserImageUrl' => $this->teaser_image_url,
];
}
Most helpful comment
Dealing with camelCase everywhere e.g. JavaScript, backend etc... And having snake_case database tables is really frustrating