我正在尝试将动态创建的输入(行)中的值存储到数据库中。但是只在数据库中输入最后一行中的最后一个值。
HTML
<div class="col-md-9 col-lg-9 schedule" ng-controller="WorkScheduleCtrl">
<div class="panel panel-default">
<form action="{{Url::to('schedule/addschedule')}}" method="post" name="employeeSchedule" class="form-horizontal form-border" novalidate>
<div class="panel-heading">
Work Schedules
</div>
<div class="panel-body"></div>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Employee</th>
<th>Shift</th>
<th>Location</th>
<th>Start Date</th>
<th>End Date</th>
<th class="text-center" style="border-top: 1px solid #ffffff; border-right: 1px solid #ffffff;"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in schedules.schedule">
<td data-ng-class="{'has-error': employeeSchedule.employee.$invalid && employeeSchedule.employee.$dirty}">
<select class="form-control input-sm" name="employee[]" ng-model="employee" ng-options="employee.employeeName for employee in employees track by employee.usersId" required>
<option value="">Select Employee</option>
</select>
<span class="help-block" ng-if="employeeSchedule.employee.$error.required && employeeSchedule.employee.$dirty">Employee's name is required.
</span>
</td>
<td data-ng-class="{'has-error': employeeSchedule.employeeshift.$invalid && employeeSchedule.employeeshift.$dirty}">
<select class="form-control input-sm" name="employeeshift" ng-model="employeeshift[]" ng-options="employeeshift.title for employeeshift in employeeshifts track by employeeshift.shiftId" required>
<option value="">Select</option>
</select>
<span class="help-block" ng-if="employeeSchedule.employeeshift.$error.required && employeeSchedule.employeeshift.$dirty">Work shift is required.
</span>
</td>
<td data-ng-class="{'has-error': employeeSchedule.location.$invalid && employeeSchedule.location.$dirty}">
<select class="form-control input-sm" name="location" ng-model="location[]" ng-options="location.locationName for location in assignedlocation track by location.locationId" required>
<option value="">Select</option>
</select>
<span class="help-block" ng-if="employeeSchedule.location.$error.required && employeeSchedule.location.$dirty">Assigned location is required.
</span>
</td>
<td>
<a ng-click="removeItem($index)" class="btn btn-danger btn-sm" title="Delete Row"><i class="fa fa-trash"></i></a>
</td>
</tr>
<tr>
<a href ng-click="addItem()" class="btn btn-default btn-sm addRowBtn"><i class="fa fa-plus fa-fw"></i>Add row</a>
</tr>
</tbody>
</table>
</div>
<div class="panel-footer">
<div class="text-right">
<input type="submit" class="btn btn-success btn-sm" value="Save" ng-disabled="!employeeSchedule.$valid" />
</div>
</div>
</form>
</div>
</div>
ANGULARJS
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-----*/
}]);
}]);
LARAVEL
public function postAddschedule() {
$rules = array(
'employee' => 'required',
'employeeshift' => 'required',
'location' => 'required'
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('schedule/addschedule/')
->withErrors($validator)->withInput();
}
else {
$count = count(Input::get('employee'));
// get data
$scheduledata = array(
array(
'usersId' => Input::get('employee'),
'shiftId' => Input::get('employeeshift'),
'locationId' => Input::get('location')
)
);
for($i = 0; $i < $count; ++$i) {
DB::table('workschedule')->insert($scheduledata[$i]);
}
}// end if/else
}
我该怎么做才能将所有行的所有值存储到我的数据库中?
答案 0 :(得分:0)
我使用这种方法解决了我的问题。我想为了添加必须专门分配的数据。
// get data
$usersId = Input::get('employee');
$shiftId = Input::get('employeeshift');
$locationId = Input::get('location');
//loop through and save data
for($i = 0; $i < $count; ++$i) {
$schedule = new WorkSchedule;
$schedule->usersId = $usersId[$i];
$schedule->shiftId = $shiftId[$i];
$schedule->locationId = $locationId[$i];
$schedule->save();
}