I have a table in my database with some fields:
- id
- name
- xx
When I call an API, I get one item from my database and return it:
$table = MyTable::select('table.*')->where('id', $id)->first();
return response()->json($table, 200);
I get a json like that:
{
"id": 1,
"name": "user1",
"xx": 0
}
I can remove the xx field by adding it to he $hidden field inside the model.
How can I rename the xx in yy ?
Does it exists a simpler way than adding a getter/setter on a yy field inside my model ?
To get:
{
"id": 1,
"name": "user1",
"yy": 0
}
The first method is using
setHiddenandselect aliaslike below :Or you can map response using Associative Array but you have to map every fields like below :
Or unset like below :