I have built an API which essentially has three calls. Firstly, a call is made to retrieve a token. Next, I have a type of autocomplete feature which returns data and the coresponding table it should look up. This data is returned like so
[
{
"result": "Apples",
"table": "fruitTable"
},
{
"result": "Bread",
"table": "breadTable"
},
{
"result": "Pie",
"table": "pieTable"
}
]
My final route is like the following
Route::get('returnSelectedResult/{table}/{selected}', array('uses'=>'APIController@returnSelectedResult'));
Whereby it uses the lookup results above to get the right table and the selected option. Then I do the following
public function returnSelectedResult($table, $selected)
{
$tableData = DB::table($table)->where('search_item', $selected)->get();
return response($tableData, 200);
}
So this all works fine. However, the data in the table is flat. So the final thing returned to the user of the API is something like this
[
{
"search_item": "Bread",
"num_types": 34,
"most_ordered": 'Baggette',
"most_popular_day": 'Saturday',
"average_profit": 3.5,
}
]
What I am showing is a very cut down version, there is a lot more data returned but it is all at the same level. What I need to return to the user is something more along the lines of this
[
{
"searched" : {
"search_item": "Bread"
},
"types" : {
"num_types": 34
},
"analytics" : {
"most_ordered": 'Baggette',
"most_popular_day": 'Saturday',
"average_profit": 3.5
}
}
]
What would be the best way to achieve something like this?
Thanks