Php 如何向Laravel中雄辩的模型对象集合添加元字段

Php 如何向Laravel中雄辩的模型对象集合添加元字段,php,json,laravel,laravel-5,eloquent,Php,Json,Laravel,Laravel 5,Eloquent,我有一个设备模型和一个读数模型,它们通过多对多关系连接起来。我正在开发一个API,它将所有注册设备的数组返回给特定用户。我想要的是响应中的一个字段,它显示读数表中的最后一个条目 这是返回响应的方法。 public function getUserDevices($user_id){ $devices = Device::where('user_id',$user_id)->get(); return response()->json($devices); } [

我有一个设备模型和一个读数模型,它们通过多对多关系连接起来。我正在开发一个API,它将所有注册设备的数组返回给特定用户。我想要的是响应中的一个字段,它显示读数表中的最后一个条目

这是返回响应的方法。

public function getUserDevices($user_id){

    $devices = Device::where('user_id',$user_id)->get();
    return response()->json($devices);
}
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1
   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0
   }
]
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 45,
         "hum" : 60
     }

   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 35,
         "hum" : 76
     }

   }
]
我目前得到的回应。

public function getUserDevices($user_id){

    $devices = Device::where('user_id',$user_id)->get();
    return response()->json($devices);
}
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1
   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0
   }
]
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 45,
         "hum" : 60
     }

   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 35,
         "hum" : 76
     }

   }
]
我想要的回应。

public function getUserDevices($user_id){

    $devices = Device::where('user_id',$user_id)->get();
    return response()->json($devices);
}
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1
   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0
   }
]
[
   {
     "id": 2,
     "user_id": 1,
     "device_name": "My Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:39:35",
     "updated_at": "2018-05-13 17:56:56",
     "device_key": "60:01:94:37:D1:34",
     "actuator": 0,
     "mode": 1

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 45,
         "hum" : 60
     }

   },
   {
     "id": 3,
     "user_id": 1,
     "device_name": "Home Device",
     "device_type": "Sensor",
     "status": 1,
     "created_at": "2018-05-01 14:40:25",
     "updated_at": "2018-05-12 13:42:00",
     "device_key": "60:01:94:37:D1:35",
     "actuator": 0,
     "mode": 0

     "reading":{    <--- This is what i want in every item of the collection
         "temp": 35,
         "hum" : 76
     }

   }
]
[
{
“id”:2,
“用户id”:1,
“设备名称”:“我的设备”,
“设备类型”:“传感器”,
“地位”:1,
“创建时间”:“2018-05-01 14:39:35”,
“更新时间”:“2018-05-13 17:56:56”,
“设备密钥”:“60:01:94:37:D1:34”,
“执行器”:0,
“模式”:1
“阅读”:{你可以通过以下方式做到这一点:

public function getUserDevices($user_id){

$devices = Device::with('reading')->where('user_id',$user_id)->get();

return response()->json($devices);
}

您应该能够创建一个将结果限制为最新结果的关系,如下所示

public function latestReading()
{
    return $this->belongsToMany('Reading')
                ->orderBy('created_at', 'DESC')
                ->limit(1)
                ->first();
}

$devices = Device::with('latestReading')->where('user_id',$user_id)->get();
或: