简体   繁体   中英

Laravel add seperate key-value pair to collection JSON

I have a Laravel controller that returns a collection of items (in this case Answers):

return AnswerResource::collection($correctAns);

This returns a JSON object as expected of course. How would I go about appending an item to that object so it's more like this?

{
  "data": [
    {
      "id": "2",
      "answer_text": "True"
    },
    {
      "id": "3",
      "answer_text": "False"
    }
  ],
  "testKey": "arsnteio12345"
}

(where the testKey thing is what's added)

试试这个

return Response::json(['data '=> $correctAns, 'testKey' => 'arsnteio12345'],200);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM