1

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"
]
16
  • 1
    can you post $request->type value Commented Jun 15, 2021 at 8:49
  • umm Sorry I didnt get you $request->type value means ? Commented Jun 15, 2021 at 8:53
  • what you get in $request->type . or just dd($request->all()) Commented Jun 15, 2021 at 8:53
  • Type of vehicle Commented Jun 15, 2021 at 8:54
  • 1
    you have yo provide more detail of blade and controller method code Commented Jun 15, 2021 at 9:16

1 Answer 1

1

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();
      
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Use of foreach for inserting data is wrong way, you should first prepare array of values using foreach then insert array using insert. It will save server resources and execution time.
Oh Okay Thanks wasnt aware of that will surely do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.