noblemfd
1 month ago
207
5
Laravel

ErrorException: Undefined offset: 1 in data-bootstrap-switch

Posted 1 month ago by noblemfd

I have a code for dynamic input field array in my Leave application

Controller

public function store(StoreLeaveTypeRequest $request)
{ 
        $leavetype = new HrLeaveType();
        $leavetype->leave_type_name             = $request->leave_type_name;
        $leavetype->leave_type_code             = $request->leave_type_code;

    $leavetype->save();        

        foreach ($request->employee_type_id as $key => $employee_type_id){

    $insert_array = [
            'no_of_days'                        => $request->no_of_days[$key],
            'employee_type_id'                  => $request->employee_type_id[$key],    
            'weekend_inclusive'                 => $request->weekend_inclusive[$key],
            'holiday_inclusive'                 => $request->holiday_inclusive[$key],
            'leave_type_id'                     => $leavetype->id,
        ];

        HrLeaveTypeDetail::create($insert_array );

         } 
    }

view

   <form  action="{{route('leave.leave_types.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
       
       
       <div class="card-body">
        <div class="form-body">
        <div class="row">  

          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
              <input  type="text" name="leave_type_name" value="{{ old('leave_type_name', $leavetype->leave_type_name) }}" placeholder="Enter leave type name" class="form-control @error('leave_type_name') is-invalid @enderror">
                @error('leave_type_name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror                 
            </div>
          </div>

          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Leave Type Code:</label>
              <input  type="text" name="leave_type_code" value="{{ old('leave_type_code', $leavetype->leave_type_code) }}" placeholder="Enter leave typecode" class="form-control @error('leave_type_code') is-invalid @enderror">
                @error('leave_type_code')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror                 
            </div>
          </div>                

   <div class="col-sm-12">
            <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Weekend Inclusive</th>
                            <th scope="col">Holiday Inclusive</th>   
                            <th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>                                                               
                            <td width="10%"><input type="checkbox" name="weekend_inclusive[]"  class="form-control weekend_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>
                            <td width="10%"><input type="checkbox" name="holiday_inclusive[]"  class="form-control holiday_inclusive" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>                                
                            <td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
                         </tr>
                        </tbody>

                    </table>
        </div>

       </div>
     </div>
    </div>          
    <!-- /.card-body -->
    <div class="card-footer">
      <button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
    </div>           
       
    </form>

I am using bootstrap switch checkbox. The checkbox can either be 0 or 1

0=No and 1=Yes

When I submitted, I got this error:

production.ERROR: ErrorException: Undefined offset: 1

and it poits to this lines in the controller:

            'weekend_inclusive'                 => $request->weekend_inclusive[$key],
            'holiday_inclusive'                 => $request->holiday_inclusive[$key],

This has to do with the checkboxes.

How do I resolve it?

Thanks

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