Question:
How to get Get Json array from multiple table in PHP?

Summary: Here is the PHP code to retrieve data from a database table named device and another table named device_category. It then structures this data into a JSON response where devices are grouped by their category.


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

}


Explanation: 

To retrieve data from multiple tables and convert it into a JSON array in PHP, you can follow these general steps:

  • Data Retrieval: The code uses Eloquent ORM to select specific columns (id_device, name_device) from the device table and (id_category, name_category) from the device_category table.

  • Join Operation: It performs an inner join between the device and device_category tables based on the id_category and category_device columns respectively.

  • Data Grouping: The retrieved devices are grouped by their name_category using the groupBy method, resulting in an associative array with category names as keys and collections of devices as values.

  • Data Restructuring: It iterates over the grouped devices, extracting category information from the first device in each category, and maps each device to an array containing its id_device, name_device, and id_category.

  • JSON Response: Finally, the restructured data is returned as a JSON response with HTTP status code 200 using Laravel's response helper function and json method.


Answered by: >Dava Gordon

Credit: >Stackoverflow


Suggested blogs:

>How to manage the Text in the container in Django?

>Activating Virtual environment in visual studio code windows 11

>Fix webapp stops working issue in Django- Python webapp

>Creating a form in Django to upload a picture from website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django


Submit
0 Answers