Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alidnet's avatar

Json Structure from Controller

Sorry if this in an obvious question, I'm trying to achieve the below result json from a function within a controller, the data will come from 3 separate tables, property, units, images.

I'm not sure how to approach this, any help will be greatly appreciated.

{
    "property": {
        "id": "1",
        "address": "123 Street1",
        "units": [{
                "unitid": "1",
                "propid": "1",
                "name": "suite 1",
                "size": 500
            },
            {
                "unitid": "2",
                "propid": "1",
                "name": "suite 2",
                "size": 200
            },
            {
                "unitid": "3",
                "propid": "1",
                "name": "suite 3",
                "size": 340
            }
        ],
        "images": [{
                "imageid": "1",
                "propid": "1",
                "src": "Images/Sun.png",
                "name": "sun1"
            },
            {
                "imageid": "2",
                "propid": "1",
                "src": "Images/Sun2.png",
                "name": "sun2"
            },
            {
                "imageid": "3",
                "propid": "1",
                "src": "Images/Sun3.png",
                "name": "sun3"
            }
        ]
    }
}
0 likes
2 replies
sairum's avatar
sairum
Best Answer
Level 1

try to merge all three tables data with

$step2 = array_merge($table1data,$table2data);
$final= array_merge($step2,$table3data);

or if you are using relationships btw them then use the with() method in query

Alidnet's avatar

I tried that but i get message: "array_merge(): Argument #1 is not an array" below are my queries

        $Props = DB::table('hq_to_manco')
        ->join('unit', 'unit.id', '=', 'hq_to_manco.unitid')
        ->join('property', 'property.id', '=', 'unit.propid')
        ->join('masterarea', 'masterarea.id', '=', 'property.areaid')
        ->join('masterregion', 'masterregion.id', '=', 'masterarea.region_id')
        ->leftJoin('portfolios_propcheck', 'portfolios_propcheck.propid', '=', 'property.id')
        ->distinct('unit.propid')
        ->select('property.id as propid', 'property.address', 'masterarea.area', 'masterregion.region', 'portfolios_propcheck.isactive' )
        ->where('hq_to_manco.PORTID', '=', $id)
        ->where('hq_to_manco.isActive', '=', '1')
        ->orderBy('masterregion.region', 'desc')
        ->get();    

        $PropIds = $Props ->pluck('propid');  

        $PropPics = DB::table('hq_to_manco')
        ->join('unit', 'unit.id', '=', 'hq_to_manco.unitid')
        ->join('property', 'property.id', '=', 'unit.propid')
        ->leftJoin('gallery', 'gallery.propid', '=', 'property.id')
        ->leftJoin('portfolios_propcheck', 'portfolios_propcheck.propid', '=', 'property.id')
        ->distinct('unit.propid')
        ->select('property.id as propid', 'gallery.image' )
        ->whereIn('property.id', $PropIds)
        ->where('hq_to_manco.isActive', '=', '1')
        ->where('gallery.webpic', '=', '1')
        ->where('gallery.type', '=', '3')
        ->get();

        $Units = DB::table('hq_to_manco')
        ->join('unit', 'unit.id', '=', 'hq_to_manco.unitid')
        ->leftJoin('users', 'users.id', '=', 'unit.activebroker')
        ->select('unit.*', 'users.photo', 'users.name', 'users.surname', 'hq_to_manco.hqid')
        ->whereIn('unit.propid', $PropIds)
        ->where('hq_to_manco.portid', '=', $id)
        ->where('hq_to_manco.isActive', '=', '1')
        ->where('unit.deleted', '=', '0')
        ->orderBy('unit.id', 'desc')
        ->get();

$ProUni = array_merge($Props,$Units);
        $final= array_merge($ProUni,$PropPics);

Please or to participate in this conversation.