Kanchan186
4 weeks ago
118
2
Laravel

how to store multiple checkboxes values in same table at same time

Posted 4 weeks ago by Kanchan186

addServiceStation.blade.php

 <div class="form-group">
                                        <h5>Choose Segment<span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                @foreach($segment as $sg)
                                <ul><li>
                                <input type="checkbox" name="sg[{{$sg->segment_id}}]"value ="{{$sg->segment_id}}" id="{{$sg->segment_id }}">

                                <label  for="{{ $sg->segment_id }}">
                                <p>{{$sg->segment_name }}</p></label></li></ul>
                                
                                @endforeach
                                        </div>
                                       
                                    </div>


                                    <div class="form-group">
                                        <h5>Choose Brands<span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                @foreach($brand as $br)
                                <ul><li>
                                <input type="checkbox" name="br[{{$br->brand_id}}]"value ="{{$br->brand_id}}" id="{{$br->brand_id }}">

                                <label  for="{{ $br->brand_id }}">
                                <p>{{$br->brand_name }}</p></label></li></ul>
                                
                                @endforeach
                                        </div>
                                       
                                    </div>

MechanicServiceStationInfoController.php

  public function store(Request $request)
    {
        //dd(request()->all());
 $this->validate($request,[
                            'service_station_email'=>'unique:mechanic_service_station_infos|required',
                                
                               ]);

           if($request->hasFile('service_station_photo')){
            

            $file = $request->file('service_station_photo');

            $destinationPath = public_path().'/service_station_photos/';
            $filename = $file->getClientOriginalName();
            if(!File::exists($destinationPath))
             {
                File::makeDirectory($destinationPath, $mode = 0777, true);
                $file->move($destinationPath, $filename);
             }
             else
             {
                $file->move($destinationPath, $filename);
             }
            

         //   echo  $filename;exit;
            }//end of if


    foreach(request('sg') as $sg_id) 
       { 
     
      foreach(request('br') as $br_id) 
       {
        MechanicServiceStationInfo::create([

        //'mechanic_id'=> auth()->id(), 
        'mechanic_id'=> 1,
        'service_station_photo' =>$filename,    
        'service_station_name' => request('service_station_name'),
        'establish_date' => request('establish_date'),
        'service_station_email' => request('service_station_email'),
        'service_station_contact' => request('service_station_contact'),
        'service_station_country' => request('service_station_country'),
        'service_station_state' => request('service_station_state'),
        'service_station_city' => request('service_station_city'),
        'service_station_address' => request('service_station_address'),
        'service_station_pincode' => request('service_station_pincode'),
        'login_first' => 0,
        'status'=>1,

        'segment_id'=>$sg_id,
        'brand_id'=>$br_id,
        
        
        ]);
     }

    }   
      array($request->get('segment_id'));
      array($request->get('brand_id'));

        
//dd('hi');
        
        return redirect('mechanic_service_station_info/view');
    }

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