我有一个包含输入字段的表,我必须从中获取信息并保存它,但我还必须从表中的每一行获取信息。我有一个整个表的提交按钮。
我需要获取$student
的ID和$subject
的ID。我怎么能得到它们?
我发布了表格
<table class="table table-responsive table-striped" id="admin-table">
<thead>
<tr>
<th>Клас:</th>
<th>N:</th>
<th>Име:</th>
<th>Предмет:</th>
<th>Оценка:</th>
<th>Тип на оценката:</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
@foreach($class as $classes)
@foreach($sub as $subject)
<tr>
<td>
{{$classes->name}}
</td>
<td>
{{$student->number_in_class}}
</td>
<td>
{{$student->full_name}}
</td>
<td>
{{$subject}}
</td>
{!! Form::open(['action' => 'Educator\AccountController@markStudent', 'class' => 'form-horizontal']) !!}
<td>
{!! Form::text('mark',null, ['class'=>'form-control col-md-2']) !!}
</td>
<td>
{!! Form::select('markType', $markType, null, ['class'=>'form-control']) !!}
</td>
</tr>
@endforeach
@endforeach
@endforeach
</tbody>
</table>
<div align="center">
<a href="{{url('educator/class-subject')}}"><button type="button" class="btn btn-default">Назад</button></a>
{!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
控制器中的方法:
public function postIndex(Request $request)
{
$user = Auth::user();
ClassSubject::where([
['subject_id', $request->input('subject')],
['class_id', $request->input('class')]
])->get();
return view('educator.account.input', [
'user' => $user,
'class'=> Clas::where('id', '=', $request->get('class'))
->get(),
'students' => User::where('class_id', '=', $request->get('class'))
->orderBy('first_name', 'asc')
->get(),
'markType' => MarkType::get()
->pluck('type', 'id'),
'sub' => Subject::where('id', '=', $request->input('subject'))
->get()
->pluck('name','id')
]);
}
我保存所需数据的方法:
public function markStudent(Request $request)
{
$validator = Validator::make($request->all(), [
'mark' => 'numeric|required|min:2|max:6',
]);
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator)
->withInput();
}
StudentMark::create([
'student_id' => the student id which i have to get from the table in the view,
'subject_id' => $request->the subject id which i have to get from the table in the view,
'mark_type_id' => $request->input('markType'),
'mark' => $request->input('mark')]);
$user = Auth::user();
$subject = ClassSubject::where('teacher_id', $user->id)
->get()
->pluck('subject_id', 'id');
return view('educator.account.marks', [
'user' => Auth::user(),
'marks' => StudentMark::whereIn('subject_id',$subject)
->orderBy('created_at', 'desc')
->get(),
]);
}
答案 0 :(得分:1)
向上移动Form::open
,在@foreach
子句之后但在数据之前放置一些:
然后将布局更改为:
<td>
{!! Form::label('classes_name', $classes->name) !!}
</td>
<td>
{!! Form::label('student_number_in_class', $student->number_in_class) !!}
</td>
<td>
{!! Form::label('student_full_name', $student->full_name) !!}
</td>
<td>
{!! Form::label('subject', $subject) !!}
</td>
然后像往常一样访问此信息:
$request->input('student_number_in_class'),
我没有测试过,但这应该可行。如果label
不适合您,请尝试使用其他表单类型。
答案 1 :(得分:1)
只需在保存结果的控制器方法上添加此查询即可。
$studentid = Student::where('colname','=',colvalue)->select('id')->first();
$subjectid = Subject::where('colname','=',colvalue)->select('id')->first();
您在视图中传递给控制器的任何数据。使用该信息获取学生和主题的ID。我无法得到确切的解决方案,因为我不知道数据库中每个表的表或列名之间的关系。