输入数据并弄清楚为什么我的数据没有存储到数据库时,我遇到了麻烦。我尝试使用资源路由和自定义路由代码,但似乎仍然没有任何效果。单击提交似乎只是刷新页面,没有错误显示
这是我的表格:
<div class="container">
<form action="{{ route('lessons.store') }}" method="post">
@csrf
<input class="d-none" name="courses_id" value="{{$course->id}}">
<div class="row">
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="name">Title</label><br>
<input type="text" name="name" id="name" class="w-100 form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" value="{{ old('name') }}" required>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('name') }}</strong></span>
@endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="status">Status</label><br>
<select name="status" id="status" class="w-100 form-control{{ $errors->has('status') ? ' is-invalid' : '' }}" value="{{ old('status') }}" required>
<option value="Active">Active</option>
<option value="Deactive">Deactive</option>
</select>
@if ($errors->has('status'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('status') }}</strong></span>
@endif
</div>
</div>
<div class="col col-lg-12">
<div class="form-group">
<label>Subject : </label>
</div>
</div>
<div class="col col-lg-12">
<textarea id="editor" name="subject" class="w-100 form-control{{$errors->has('subject') ? ' is-invalid' : ''}}" id="subject" rows="3" value="{{old('subject')}}"></textarea>
@if ($errors->has('subject'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('subject') }}</strong></span>
@endif
</div>
</div>
<div class="modal-footer pb-0">
<div class="float-right">
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Discard</button>
<button type="submit" class="btn btn-sm btn-primary">Save</button>
</div>
</div>
</form>
</div>
我在SubjectCourse中的存储功能:
public function store(Request $request)
{
$request->validate([
'title' => 'required|string',
'status' => 'required'
]);
$courseSubject = new CourseSubject([
'user_id' => $request->get('user_id'),
'title' => $request->get('title'),
'status' => $request->get('status'),
'subject' => $request->get('subject'),
]);
if($courseSubject->save()) {
return \App::make('redirect')->back()->refresh()->with('flash_success', 'Success!');
} else {
return \App::make('redirect')->back()->refresh()->with('flash_error', 'Error!');
}
}
这是我的路线:
Route::group(['middleware' => ['role:Teacher']], function () {
Route::get('/t', function() {
return view('dashboard.teacher.teacher');
});
Route::resource('t/courses', 'CoursesController');
Route::get('t/courses', 'CoursesController@index')->name('courses.index');
Route::resource('t/courses/lesson', 'SubjectCourses')->only('store');
Route::get('t/courses/{id}/lessons', 'SubjectCourses@index')->name('lessons.index');
Route::post('/t/courses/lessons/add', 'SubjectCourses@store')->name('lessons.store');
});
同样,我完全没有收到任何错误消息,只是提交按钮什么也不做。非常感谢!
答案 0 :(得分:0)
问题出在您的验证中。您的字段名称是名称,但是您正在控制器中将其验证为标题。改变这个
<input type="text" name="name" id="name" class="w-100 form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" value="{{ old('name') }}" required>
收件人
<input type="text" name="title" id="title" class="w-100 form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}" required>
答案 1 :(得分:0)
在用户存储功能中使用此功能:
$courseSubject = CourseSubject::create([
'user_id' => $request->get('user_id'),
'title' => $request->get('title'),
'status' => $request->get('status'),
'subject' => $request->get('subject'),
]);
不使用实例,而使用CourseSubject::create
代替new CourseSubject
。
将控制器或刀片服务器中的领域名称正确化。他们不匹配。