dilfdo
3 years ago

creating custom laravel array

Posted 3 years ago by dilfdo

please advice me how to make an array like that using following 3 queries.


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

this is how i want to out out the results.
i want to display preCheck and postCheck for each shift_id like below.
how can i proceed this

{
  "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
        }
      ]
    },

Please sign in or create an account to participate in this conversation.