从多个表中获取Json数组

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

我的数据库中有两个表。

device_category('id_category', 'name_category');
device('id_device', 'name_device', 'category_device');

我有一个

DeviceController
get()
方法:

public function get()
{
    $devices = Device::select('*')
                 ->join('device_category','device_category.id_category','=','device.category_device')
                 ->get();

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

JSON结果为:

[
    {
        "id_category": 1,
        "name_category": "Phone",
        "id_device": 1,
        "name_device": "iPhone 10"
    },
    {
        "id_category": 1,
        "name_category": "Phone",
        "id_device": 2,
        "name_device": "iPhone XS"
    },
    {
        "id_category": 2,
        "name_category": "Computer",
        "id_device": 3,
        "name_device": "Lenovo xx"
    }
]

然后我对结果进行分组:

$devices = $devices->groupBy('name_category');
return response()->json($devices, 200);

并得到这个 JSON:

{
    "Phone" : [
        {
            "id_category": "1",
            "id_device": 1,
            "name_device": "iPhone 10"
        },
        {
            "id_category": "1",
            "id_device": 2,
            "name_device": "iPhone XS"
        }
    ],
    "Computer" : [
        {
            "id_category": "2",
            "id_device": 3,
            "name_device": "Lenovo XX"
        }
    ]
}

我想要一个类别数组,每个类别都有一个设备数组,我怎样才能得到这个结果?我可以在一个查询中完成它吗?或者我需要获取类别然后设置设备?

[
    {
        "id_category": 1,
        "name_category": "Phone",
        "device" : [
            {
                "id_device": 1,
                "id_category": 1,
                "name_device": "iPhone 10"
            },
            {
                "id_device": 2,
                "name_device": "iPhone XS"
            }
        ]
    },
    {
        "id_category": 2,
        "name_category": "Computer",
        "device" : [
            {
                "id_device": 3,
                "name_device": "Lenovo XX"
            }
        ]
    }
]
php json lumen
1个回答
0
投票

如果你不想定义关系,我必须同意@apokryfos 位,那么以下应该会给你你想要的结果

public function get()
{
    $devices = Device::select(
                    'device.id_device',
                    'device.name_device',
                    'device_category.id_category',
                    'device_category.name_category'
                )
                ->join('device_category', 'device_category.id_category', '=', 'device.category_device')
                ->get();

    // Group the devices by category
    $groupedDevices = $devices->groupBy('name_category');

    // Restructure the data
    $result = [];
    foreach ($groupedDevices as $category => $categoryDevices) {
        $categoryInfo = $categoryDevices->first();
        $categoryData = [
            'id_category' => $categoryInfo->id_category,
            'name_category' => $categoryInfo->name_category,
            'device' => $categoryDevices->map(function ($device) {
                return [
                    'id_device' => $device->id_device,
                    'name_device' => $device->name_device,
                    'id_category' => $device->id_category,
                ];
            })->all(),
        ];
        $result[] = $categoryData;
    }

    return response()->json($result, 200);
}
© www.soinside.com 2019 - 2024. All rights reserved.