Php 修改数组json api响应laravel

Php 修改数组json api响应laravel,php,json,laravel,api,Php,Json,Laravel,Api,我是拉雷维尔的新手,请帮助我解决这个问题。我需要编辑image属性上的响应以作为URL返回,而不仅仅是存储在数组响应上的数据库中的图像名称 以下是我的源代码: /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function serv_list(){ $services = Add_Service::all(); return $service

我是拉雷维尔的新手,请帮助我解决这个问题。我需要编辑image属性上的响应以作为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"
    }
]
我如何需要回复:

[
    {
        "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"
}
为此,您必须使用basic

$services->transform(function($q){
    $q->image = 'https://google.com/avatars/' . $q->image;
    return $q;
});
为此,您必须使用basic

$services->transform(function($q){
    $q->image = 'https://google.com/avatars/' . $q->image;
    return $q;
});