如何过滤数组以获取 Laravel 中两个不同对象中的特定列

问题描述 投票:0回答:3

我需要这样的回应。

"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;
arrays laravel eloquent laravel-8 query-builder
3个回答
1
投票

试试这个

$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;

0
投票

我尝试了很多次,终于得到了这个。

$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]]]);
        }

0
投票

如果要实现完全方法链式脚本,请使用

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();
© www.soinside.com 2019 - 2024. All rights reserved.