你好,我在laravel中制作了一个GET
API,它显示了来自数据库的值,我试图使返回的json结果具有一个头标记,一个例子就是这个公共api
https://dummy.restapiexample.com/api/v1/employees
正如你所看到的,这个API显示了status:success
,然后是data:
,这是我的代码:
function getDevice()
{
return Client::all();
}
这就是我在《 Postman 》中得到的结果:
data [ //here to have a header
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian@example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby@example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison@example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore@example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold@example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie@example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms@example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer@example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93@example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie@example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test@example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]
What im trying to do is:
[
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian@example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby@example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison@example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore@example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold@example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie@example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms@example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer@example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93@example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie@example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test@example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]
2条答案
按热度按时间jobtbby31#
osh3o9ms2#
请参考以下解决方案以获得每个响应:
return response()-〉json('status '=〉' success ',' message '=〉'客户端列表成功。','data'=〉客户端::all());
如果您知道如何使用Laravel中的帮助器功能,请使用以下所有回答:
示例:
在控制器中,只需添加以下行:
我想这对你会有很大帮助。