I am new to laravel 4 and I am trying to create a rest API following best practices defined by Apigee.
One of the best practice defined by apigee is to use camel case for json attribute keys, this way when using the API in Javascript the corresponding objects will follow attributes code convention (camel case).
I want to be able to define datatable columns following snake case but when retrieving eloquent objects though my api, the corresponding JSON has to follow camel case.
I read about a static variable $snakeAttributes that could be set in the model class and its documentation says "Indicates whether attributes are snake cased on arrays". I tried to override this variable and set it to false (MyResource class) but when executing the folowing code, the json still comes in snake case:
Code:
$resource = MyResource::find($id);
return Response::json($resource);
JSON:
{
first_name: 'Michael',
created_at: "2013-10-24 15:30:01",
updated_at: "2013-10-24 15:30:01"
}
Does someone have an idea on how to solve that?
Create BaseModel and a new method to help you with it:
Your model:
And then use it:
The way I see, you'll have to make a array, work manually on the keys (camel case) and then convert the array (not the result) on a JSON.
I guess that will do the job. :D