我正在使用 Laravel框架中的表单,我希望当用户点击提交按钮时,表单字段中的内容将提交给控制器,控制器稍后会将其保存在数据库中。我正在使用 jquery从表单中提取数据。但问题是数据没有提交到数据库中,我试图通过首先在控制台选项卡中记录数据调试但没有提交任何内容 ...请协助?
查看
<form action="{{ route('createClass') }}" class="form-horizontal" id="frm-create-class" method="POST">
<input type="hidden" name="active" id="active" value="1">
{{ csrf_field() }}
<div class="panel-body">
<div class="form-group">
{{-----------------------------------}}
<div class="col-sm-3">
<label for="academic-year"> Academic Year</label>
<div class="input-group">
<select class="form-control" name="acdemic_id" id="academic_id">
@foreach($academics as $key => $y)
<option value="{{ $y->academic_id}}"> {{ $y->academic }} </option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-academic"></span>
</div>
</div>
</div>
{{-----------------------------}}
<div class="col-sm-4">
<label for="academic-year"> Course</label>
<div class="input-group">
<select class="form-control" name="program_id" id="program_id">
<option value="">---------------</option>
@foreach($programs as $key => $p)
<option value="{{ $p->program_id }}"> {{ $p->program }} </option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-program"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-5">
<label for="academic-year"> Level</label>
<div class="input-group">
<select class="form-control" name="level_id" id="level_id">
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-level"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-3">
<label for="academic-year"> Shift</label>
<div class="input-group">
<select class="form-control" name="shift_id" id="shift_id">
@foreach($shift as $shf)
<option value="{{ $shf->shift_id }}"> {{ $shf->shift }}</option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-shift"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-4">
<label for="time"> Time</label>
<div class="input-group">
<select class="form-control" name="time_id" id="time_id">
@foreach($time as $t)
<option value="{{ $t->time_id }}"> {{ $t->time }}</option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-time"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-3">
<label for="batch"> Batch</label>
<div class="input-group">
<select class="form-control" name="batch_id" id="batch_id">
@foreach($batch as $b)
<option value="{{ $b->batch_id }}"> {{ $b->batch }}</option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-Batch"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-2">
<label for="group"> Group</label>
<div class="input-group">
<select class="form-control" name="group_id" id="group_id">
@foreach($group as $g)
<option value="{{ $g->group_id }}"> {{ $g->groups }}</option>
@endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-Group"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-3">
<label for="startDate"> Start Date</label>
<div class="input-group">
<input type="text" class="form-control" name="start_date" id="start_date">
<div class="input-group-addon">
<span class="fa fa-calendar"></span>
</div>
</div>
</div>
{{---------------------------------------}}
<div class="col-sm-4">
<label for="endDate"> End Date</label>
<div class="input-group">
<input type="text" class="form-control" name="end_date" id="end_date">
<div class="input-group-addon">
<span class="fa fa-calendar"></span>
</div>
</div>
</div>
</div> <!-- END FORM GROUP-->
</div> <!-- END PANEL BODY-->
<div class="panel-footer">
<button class="btn btn-default btn-sm" type="submit" > Create Course </button>
</div>
</form>
Jquery代码将数据提交给控制器并将其记录在控制台选项卡
中$('#frm-create-class').on('submit', function(e){
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr('action');
$.post(url, data, function(data){
console.log(data);
})
$(this).trigger('reset');
});
CourseController 控制器文件
public function createClass(Request $request){
if($request->ajax()){
return (MyClass::create($request->all()));
}
}
路线
Route::post('/manage/course/class' , 'CourseController@createClass')->name('createClass');
移植
Schema::create('classes', function (Blueprint $table) {
$table->increments('class_id');
$table->integer('academic_id')->unsigned();
$table->integer('level_id')->unsigned();
$table->integer('shift_id')->unsigned();
$table->integer('time_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->integer('batch_id')->unsigned();
$table->date('start_date');
$table->date('end_date');
$table->boolean('active');
$table->timestamps();
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('shift_id')->references('shift_id')->on('shifts');
$table->foreign('time_id')->references('time_id')->on('times');
$table->foreign('group_id')->references('group_id')->on('groups');
$table->foreign('batch_id')->references('batch_id')->on('batches');
});