SSMorgan
SSMorgan

Reputation: 103

Trying to access array offset on value of type null in Laravel while adding multiple values in database

I have a table Vehicle I am trying to save multiple values.

I have columns: id, vehicleno, type, make, helmet, seatbelt, cleaner, type_taxi

In this columns (make, helmet, seatbelt, cleaner, type_taxi) are set to nullable

For example 1 if the make is selected as car only seatbelt column will be filled else will be null

Example 2 if make is selected as motorcycle then only helmet column will be filled else will be null

And there can be many vehicles and for each vehicles there will be these set of questions

Controller

 public function insert( Request $request)
    {
        
        
        //accident
            $accident = new Accident();
            $accident->branch_id = $request->input('branch');
        $accident->roadname = $request->input('roadname');
        $accident->date = $request->input('dtb_yr');
        $accident->time = $request->input('appt');      
        $accident->no_of_vehicle = $request->input('noofvehicle');
        $accident->injured = $request->input('injured');
        $accident->death = $request->input('death');
        $accident->hit_and_run = $request->input('hitandrun');
        $accident->construction = $request->input('construction');
        $accident->casereg = $request->input('caseregister');
        $accident->road_maintain = $request->input('road_maintain');
        $accident->IPC_MVAct_Sections = $request->input('ipmvc');
        $accident->save();

       

        
        //vehicle
        foreach ($request->type as $key => $type)
           
        {
          $vehicle = new Vehicle();
          $vehicle->type = $type;
          $vehicle->vehicleno = $request->vehicleno;
          $vehicle->make = $request->make;
          $vehicle->helmet = $request->helmet;
          $vehicle->seatbelt = $request->seatbelt;
          $vehicle->cleaner = $request->cleaner;
          $vehicle->type_taxi = $request->type_taxi;
          $vehicle->accidents_id=$accident->id;
          $vehicle->save();
      
        }

dd($request->all());

array:25 [▼
  "_token" => "tWqfGWJVtRGmN7MBuHfARjLgliIHrDMcFejli4th"
  "branch" => "1"
  "roadname" => "Sion Panvel Special State Highway Vashi Creek Bridge to Kalamboli Junction"
  "dtb_yr" => "2021-06-15"
  "appt" => "14:26"
  "latitude" => "19.120127999999998"
  "longitude" => "72.8891392"
  "injured" => "2"
  "death" => "2"
  "noofvehicle" => "2"
  "vehicleno" => "MH054BBC4"
  "type" => array:1 [▼
    0 => "Motorcycle"
  ]
  
  "construction" => "yes"
  "road_maintain" => "MMRDA"
  "caseregister" => "yes"
  "ipmvc" => "IPC 34"
  "submit" => "Submit"
]

Upvotes: 1

Views: 138

Answers (1)

SSMorgan
SSMorgan

Reputation: 103

I just updated my controller to: Check whether the value entering is null or not and if value is not selected just set it as null

foreach ($request->type as $key => $type)
           
        {
          $vehicle = new Vehicle();
          $vehicle->type = $type;
          $vehicle->vehicleno = $request->vehicleno[$key];
          $vehicle->make = isset($request->make[$key]) ? $request->make[$key]:null;
          $vehicle->helmet = isset($request->helmet[$key]) ? $request->helmet[$key]:null;
          $vehicle->seatbelt = isset($request->seatbelt[$key]) ? $request->seatbelt[$key]:null;
          $vehicle->cleaner = isset($request->cleaner[$key]) ? $request->cleaner[$key]:null;
          $vehicle->type_taxi = isset($request->type_taxi[$key]) ? $request->type_taxi[$key]:null;
          $vehicle->accidents_id=$accident->id;
          $vehicle->save();
      
        }

Upvotes: 1

Related Questions