I have a laravel collection below from: $unitinventory = DB::select('call wh_inventory_list(?)', array('Units'));
[
{
"unit_id":"UCL2100001",
"rr_id":"RR2100001",
"make":"FAW",
"chassis_no":"LFWSRXRJ9M1E00004",
"engine_no":"CA6DM2-42E5153558354",
"body_type":"TRACTOR HEAD",
"horse_power":"420HP",
"cabin_type":"J6P E5",
"numwheels":"6W",
"unit_status":"Available",
"unit_location":null
},
{
"unit_id":"UCL2100002",
"rr_id":"RR2100002",
"make":"FAW",
"chassis_no":"LFWSRXRJ4M1E00007",
"engine_no":"CA6DM2-42E5153563283",
"body_type":"TRACTOR HEAD",
"horse_power":"420HP",
"cabin_type":"J6P E5",
"numwheels":"6W",
"unit_status":"Available",
"unit_location":null
}
]
And another collection: $modifification = collect(DB::table('pd_jo_bodymodification')->get());
[
{
"row_id":2,
"jo_id":"JO2100003",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"1st conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
},
{
"row_id":4,
"jo_id":"JO2100004",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"2nd conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
},
{
"row_id":4,
"jo_id":"JO2100004",
"jo_chassisno":"LDFDRETFGGF000RE",
"jo_convertedbody":"OTHER CONVERSION",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
}
]
I want to find out if a $unitinventory->chassis_no has a modification history so I did this:
foreach ($unitinventory as $key => $value) {
$tmpmodif["conversion"] = $modifification->where('jo_chassisno',$value->chassis_no);
}
Which returns the following:
{
"conversion":[
{
"row_id":2,
"jo_id":"JO2100003",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"1st conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
},
{
"row_id":4,
"jo_id":"JO2100004",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"2nd conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
}
]
}
Now I want to add the conversion history to $unitinventory collection to look like this:
$unitinventory =
[
{
"unit_id":"UCL2100001",
"rr_id":"RR2100001",
"make":"FAW",
"chassis_no":"LFWSRXRJ9M1E00004",
"engine_no":"CA6DM2-42E5153558354",
"body_type":"TRACTOR HEAD",
"horse_power":"420HP",
"cabin_type":"J6P E5",
"numwheels":"6W",
"unit_status":"Available",
"unit_location":null,
"conversion":[]
},
{
"unit_id":"UCL2100002",
"rr_id":"RR2100002",
"make":"FAW",
"chassis_no":"LFWSRXRJ4M1E00007",
"engine_no":"CA6DM2-42E5153563283",
"body_type":"TRACTOR HEAD",
"horse_power":"420HP",
"cabin_type":"J6P E5",
"numwheels":"6W",
"unit_status":"Available",
"unit_location":null,
"conversion":[
{
"row_id":2,
"jo_id":"JO2100003",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"1st conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
},
{
"row_id":4,
"jo_id":"JO2100004",
"jo_chassisno":"LFWSRXRJ4M1E00007",
"jo_convertedbody":"2nd conversion",
"jo_mileage":"test",
"jo_serviceadvisor":"test",
"jo_servicerequest":"test",
"jo_remarks":"test"
}
]
}
]
How to make it possible? Thanks!
Advertisement
Answer
I solve the problem with this approach:
foreach ($tmpmodif as $key => $value) {
$unitinventory[$key] = array_merge( (array)$value, (array)["conversion"=>$modifification->where('jo_chassisno',$value->chassis_no)]);
}
Merging array inside foreach.