我需要这样的回应。
"result": [
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
]
我的代码在下面。我只想从我的整个回复中分离出两个字段“lat”和“lng”。我的 sql 查询是正确的,但我想创建上面提到的自定义响应
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$array = [];
foreach ($get1 as $key => $value) {
array_push($array, ["properties" => $value, "geometry" => $value->lat]);
}
return $array;
试试这个
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$main_array = [];
foreach ($get1 as $key => $value) {
$main_array[$key]['properties'] = array('device_id' => $value->device_id, 'device_name' => $value->device_name, 'state' => $value->state, 'status' => $value->status);
$main_array[$key]['geometry'] = array('lat' => $value->lat, 'lng' => $value->lng);
}
return $main_array;
我尝试了很多次,终于得到了这个。
$get = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$finalarray =[];
foreach ($get as $key => $value) {
array_push($finalarray, ["properties" => ['device_id' => $value->device_id, 'name' => $value->name, 'status' => $value->status, 'state' => $value->state, 'type' => $value->type, ], "geometry" => ["coordinates" => ['lat' => $value->lat, 'lng' => $value->lng]]]);
}
如果要实现完全方法链式脚本,请使用
map()
形成所需的子数组,将结果集数据转换为数组,使用 result
分配 put()
的父键,然后转换整个有效负载到 JSON。
return collect()
->put(
'result',
DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get()
->map(fn($row) => [
"properties" => [
'device_id' => $row->device_id,
'name' => $row->name,
'status' => $row->status,
'state' => $row->state,
'type' => $row->type,
],
"geometry" => [
"coordinates" => [
'lat' => $row->lat,
'lng' => $row->lng,
],
],
])
->toArray()
)
->toJson();