title: Store values in dynamically created input fields using laravel
body: <p>I am trying to store values from dynamically created inputs(rows) to database. But only the last values in the last row is entered in the database.</p>
  
  <hr>
  
  <p><strong>HTML</strong></p>
  
  <pre><code> &lt;div class="col-md-9 col-lg-9 schedule" ng-controller="WorkScheduleCtrl"&gt;
     &lt;div class="panel panel-default"&gt;
         &lt;form action="{{Url::to('schedule/addschedule')}}" method="post" name="employeeSchedule" class="form-horizontal form-border" novalidate&gt;
          &lt;div class="panel-heading"&gt;
              Work Schedules
          &lt;/div&gt;
          &lt;div class="panel-body"&gt;&lt;/div&gt;
          &lt;div class="table-responsive"&gt;
              &lt;table class="table table-hover"&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
                      &lt;th&gt;Employee&lt;/th&gt;
                      &lt;th&gt;Shift&lt;/th&gt;
                      &lt;th&gt;Location&lt;/th&gt;
                      &lt;th&gt;Start Date&lt;/th&gt;
                      &lt;th&gt;End Date&lt;/th&gt;
                      &lt;th class="text-center" style="border-top: 1px solid #ffffff; border-right: 1px solid #ffffff;"&gt;&lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
  
                      &lt;tr ng-repeat="item in schedules.schedule"&gt;
                          &lt;td data-ng-class="{'has-error': employeeSchedule.employee.$invalid &amp;&amp; employeeSchedule.employee.$dirty}"&gt;
                              &lt;select class="form-control input-sm" name="employee[]" ng-model="employee" ng-options="employee.employeeName for employee in employees track by employee.usersId" required&gt;
                                  &lt;option value=""&gt;Select Employee&lt;/option&gt;
                              &lt;/select&gt;
  
                              &lt;span class="help-block" ng-if="employeeSchedule.employee.$error.required &amp;&amp; employeeSchedule.employee.$dirty"&gt;Employee's name is required.
                              &lt;/span&gt;
  
                          &lt;/td&gt;
  
                          &lt;td data-ng-class="{'has-error': employeeSchedule.employeeshift.$invalid &amp;&amp; employeeSchedule.employeeshift.$dirty}"&gt;
                              &lt;select class="form-control input-sm" name="employeeshift" ng-model="employeeshift[]" ng-options="employeeshift.title for employeeshift in employeeshifts track by employeeshift.shiftId" required&gt;
                                  &lt;option value=""&gt;Select&lt;/option&gt;
                              &lt;/select&gt;
  
                              &lt;span class="help-block" ng-if="employeeSchedule.employeeshift.$error.required &amp;&amp; employeeSchedule.employeeshift.$dirty"&gt;Work shift is required.
                              &lt;/span&gt;
                          &lt;/td&gt;
  
                          &lt;td data-ng-class="{'has-error': employeeSchedule.location.$invalid &amp;&amp; employeeSchedule.location.$dirty}"&gt;
                              &lt;select class="form-control input-sm" name="location" ng-model="location[]" ng-options="location.locationName for location in assignedlocation track by location.locationId" required&gt;
                                  &lt;option value=""&gt;Select&lt;/option&gt;
                              &lt;/select&gt;
  
                              &lt;span class="help-block" ng-if="employeeSchedule.location.$error.required &amp;&amp; employeeSchedule.location.$dirty"&gt;Assigned location is required.
                              &lt;/span&gt;
                          &lt;/td&gt;
  
                          &lt;td&gt;
                              &lt;a ng-click="removeItem($index)" class="btn btn-danger btn-sm" title="Delete Row"&gt;&lt;i class="fa fa-trash"&gt;&lt;/i&gt;&lt;/a&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;a href ng-click="addItem()" class="btn btn-default btn-sm addRowBtn"&gt;&lt;i class="fa fa-plus fa-fw"&gt;&lt;/i&gt;Add row&lt;/a&gt;
                      &lt;/tr&gt;
  
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
  
          &lt;div class="panel-footer"&gt;
              &lt;div class="text-right"&gt;
                  &lt;input type="submit" class="btn btn-success btn-sm" value="Save" ng-disabled="!employeeSchedule.$valid" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>ANGULARJS</strong></p>
  
  <pre><code>app.controller('WorkScheduleCtrl', [ '$scope', '$http', function ($scope, $http) {
  
       /*-----create and remove rows dynamically-----*/
       // assign default values
       $scope.schedules = {
          schedule: [{
              employee: '',
              employeeshift: '',
              location: ''
          }]
      };
      // add row
  
      $scope.addItem = function() {
  
        $scope.schedules.schedule.push({
          employee: '',
          employeeshift: '',
          location: ''
        });
  
      };
  
      // delete row
      $scope.removeItem = function(index) {
          $scope.schedules.schedule.splice(index, 1);
      };
      /*-----./create and remove rows-----*/
  
     }]);
  
  }]);
  </code></pre>
  
  <hr>
  
  <p><strong>LARAVEL</strong></p>
  
  <pre><code>public function postAddschedule() {
  
      $rules = array(
          'employee'      =&gt;  'required',
          'employeeshift' =&gt;  'required',
          'location'      =&gt;  'required'
      );
  
      // run the validation rules on the inputs from the form
      $validator = Validator::make(Input::all(), $rules);
  
      if($validator-&gt;fails()) {
  
          return Redirect::to('schedule/addschedule/')
          -&gt;withErrors($validator)-&gt;withInput();
      }
      else {
          $count = count(Input::get('employee'));
  
          // get data
          $scheduledata = array(
              array(
                  'usersId'       =&gt;  Input::get('employee'),
                  'shiftId'       =&gt;  Input::get('employeeshift'),
                  'locationId'    =&gt;  Input::get('location')
              )
          );
  
          for($i = 0; $i &lt; $count; ++$i) {
              DB::table('workschedule')-&gt;insert($scheduledata[$i]);
          }
  
      }// end if/else
  
  }
  </code></pre>
  
  <hr>
  
  <hr>
  
  <p>What should I do to in order to be able to store all values from all the rows into my database?</p>