【发布时间】:2020-06-06 07:52:22
【问题描述】:
我是 laravel 的新手,请帮助我解决这个问题。我需要编辑我对图像属性的响应以作为 URL 返回,而不仅仅是数组响应中存储在数据库中的图像名称。
这是我的源代码:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function serv_list(){
$services = Add_Service::all();
return $services;
}
目前的回应:
[
{
"id": 1,
"service_name": "Hair",
"desc": "Everything that has to do with hair and more",
"image": "1590713947.jpg",
"created_at": "2020-05-29T00:59:08.000000Z",
"updated_at": "2020-05-29T02:14:45.000000Z"
},
{
"id": 2,
"service_name": "Nails",
"desc": "Nails Services",
"image": "1590722173.jpg",
"created_at": "2020-05-29T03:16:14.000000Z",
"updated_at": "2020-05-29T03:21:09.000000Z"
}
]
我需要怎样的回应:
[
{
"id": 1,
"service_name": "Hair",
"desc": "Everything that has to do with hair and more",
"image": "https://google.com/avatars/1590713947.jpg",
"created_at": "2020-05-29T00:59:08.000000Z",
"updated_at": "2020-05-29T02:14:45.000000Z"
},
{
"id": 2,
"service_name": "Nails",
"desc": "Nails Services",
"image": "https://google.com/avatars/1590722173.jpg",
"created_at": "2020-05-29T03:16:14.000000Z",
"updated_at": "2020-05-29T03:21:09.000000Z"
}
]
我尝试了以下方法:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function serv_list(){
$services = Add_Service::all();
foreach($services as $service){
$id = $service->id;
$name = $service->service_name;
$desc = $service->desc;
$pic = $service->image;
}
$image = 'https://google.com/avatars/'.$pic;
$data = array('id'=>$id, 'name'=>$name, 'desc'=>$desc, 'image'=>$image);
return $data;
}
回复是:
{
"id": 2,
"name": "Nails",
"desc": "Nails Services",
"image": "https://google.com/avatars/1590722173.jpg"
}
【问题讨论】: