I need response like this.
"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
}
},
]
where my code is below. I just want to sperate two fields 'lat', 'lng' from my whole response. My sql query is correct but i want to create custom response that i mentioned above
$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;
Try this
$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;