请告诉我如何使用以下3个查询制作类似的数组。
$shiftsids = Shifts::select('id','shift_no', 'bus_no', 'shift_start_time', 'shift_end_time', 'run_type')
->where('user_id', '=', $driver_id->id)
->where('week_id', '=', $week_id->id)
->where('time_sheet_id', '=', $timesheet_id->id)
->get();
$preCheck = DB::table('driver_pre_departure_items')
->join('shifts', 'shifts.id', '=', 'driver_pre_departure_items.shift_id')
->select('shift_id','item','value')
->where('driver_pre_departure_items.shift_id', '=', $shiftsid->id)
->get();
$postCheck = DB::table('driver_shift_end_items')
->join('shifts', 'shifts.id', '=', 'driver_shift_end_items.shift_id')
->select('shift_id','item','value')
->where('driver_shift_end_items.shift_id', '=', $shiftsid->id)
->get();
这就是我想要的结果。 我想为每个shift_id显示preCheck和postCheck,如下所示。 我该怎么办呢
{
"data": [
{
"shift_no": 1,
"bus_no": "",
"shift_start_time": "08:00:00",
"shift_end_time": "10:00:00",
"run_type": "S",
"driver_pre_departure_items": [
{
"pre_departure_type": 1,
"item_name": "Check all running lights & indicators",
"value": 0
},
{
"pre_departure_type": 2,
"item_name": "Look for tyres that are flat or partly flat- check wheel nuts",
"value": 0
}
],
"driver_shift_end_items": [
{
"shift_end_type": 1,
"item_name": "Record any problems in defect book",
"value": 0
},
{
"shift_end_type": 2,
"item_name": "Tell management or mechanic if there are any problems that require attention",
"value": 0
}
]
},
已更新
$array = array();
$shiftsids = Shifts::select('id','shift_no', 'bus_no', 'shift_start_time', 'shift_end_time', 'run_type')
->where('user_id', '=', $driver_id->id)
->where('week_id', '=', $week_id->id)
->where('time_sheet_id', '=', $timesheet_id->id)
->get();
foreach ($shiftsids as $shiftsid)
{
$array[]['shift_no'] = $shiftsid->shift_no;
$array[]['shift_start_time'] = $shiftsid->shift_start_time;
$array[]['shift_end_time'] = $shiftsid->shift_end_time;
$preCheck = DB::table('driver_pre_departure_items')
->join('shifts', 'shifts.id', '=', 'driver_pre_departure_items.shift_id')
->select('shift_id','item','value')
->where('driver_pre_departure_items.shift_id', '=', $shiftsid->id)
->get();
foreach ($preCheck as $pch)
{
$array[]['driver_pre_departure_items'] = array('item' => $pch->item,'value' => $pch->value);
}
$postCheck = DB::table('driver_shift_end_items')
->join('shifts', 'shifts.id', '=', 'driver_shift_end_items.shift_id')
->select('shift_id','item','value')
->where('driver_shift_end_items.shift_id', '=', $shiftsid->id)
->get();
foreach ($postCheck as $pch)
{
$array[]['driver_shift_end_items'] = array('item' => $pch->item,'value' => $pch->value);
}
}
return ['data' => $array];
新输出
{
"data": [
{
"shift_no": 1
},
{
"shift_start_time": "08:00:00"
},
{
"shift_end_time": "13:00:00"
},
{
"driver_pre_departure_items": {
"item": "Check all running lights & indicators",
"value": 0
}
},
{
"driver_pre_departure_items": {
"item": "Look for tyres that are flat or partly flat- check wheel nuts",
"value": 0
}
},
{
"shift_no": 2
},
{
"shift_start_time": "13:00:00"
},
{
"shift_end_time": "18:00:00"
},
{
"driver_pre_departure_items": {
"item": "Check all running lights & indicators",
"value": 0
}
},
{
"driver_pre_departure_items": {
"item": "Look for tyres that are flat or partly flat- check wheel nuts",
"value": 0
}
},
{
"driver_pre_departure_items": {
"item": "Check that tyres on dual wheels are not touching one another ",
"value": 0
}
},
{
"driver_pre_departure_items": {
"item": "Make sure that no liquids are leaking onto the road, or into the engine compartment",
"value": 0
}
}
}
]
}
答案 0 :(得分:0)
$array = array();
$shiftsids = Shifts::select('id','shift_no', 'bus_no', 'shift_start_time', 'shift_end_time', 'run_type')
->where('user_id', '=', $driver_id->id)
->where('week_id', '=', $week_id->id)
->where('time_sheet_id', '=', $timesheet_id->id)
->get();
foreach ($shiftsids as $shiftsid)
{
$zarray = array(
['shift_no'] => $shiftsid->id,
// ...
// ...
// other parameters
);
$preCheck = DB::table('driver_pre_departure_items')
->join('shifts', 'shifts.id', '=', 'driver_pre_departure_items.shift_id')
->select('shift_id','item','value')
->where('driver_pre_departure_items.shift_id', '=', $shiftsid->id)
->get();
foreach ($preCheck as $pch)
{
$zarray['driver_pre_departure_items'][] = array('
'pre_departure_type' => $pch->pre_departure_type;
')
}
$postCheck = DB::table('driver_shift_end_items')
->join('shifts', 'shifts.id', '=', 'driver_shift_end_items.shift_id')
->select('shift_id','item','value')
->where('driver_shift_end_items.shift_id', '=', $shiftsid->id)
->get();
foreach ($postCheck as $pch)
{
$zarray['driver_pre_departure_items'][] = array('
'pre_departure_type' => $pch->pre_departure_type;
')
}
$array[] = $zarray;
}