Home » Jquery » javascript – Passing the values of array months so that each row gets inserted in database

javascript – Passing the values of array months so that each row gets inserted in database

Posted by: admin February 22, 2020 Leave a comment

Questions:

I want to pass an array of months from the controller to the view but when I pass it in a for loop only the value of last row (i.e december) gets inserted. How can I solve this issue?

public function adminViewCreate()
{
  $employee = Employee::all();  
  $months = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
  return view('AdminLeave', compact('employee', 'months'));
}

View:

@foreach($months as $month)
  <tr>
    <td class="inner"><input type="text" name="month_{{$month}}" id="month" value="<?php echo date("Y"); ?>" class="form-control form-control-sm keypressbutton" style="border: none;text-align: center;background-color: transparent;"></td>
    <td class="inner"><input type="text" class="form-control form-control-sm keypressbutton " style="border: none;text-align: center;background-color: transparent" name="earned_leave_{{$month}}" id="earned" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
    <td class="inner"><input type="text" class="form-control form-control-sm keypressbutton "  style="border: none;text-align: center;background-color: transparent" name="casual_leave_{{$month}}" id="casual" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
    <td class="inner"><input type="text" class="form-control form-control-sm keypressbutton"  style="border: none;text-align: center;background-color: transparent" name="sick_leave_{{$month}}" id="sick" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
  </tr>
@endforeach
How to&Answer: