我想在我的主题表中有多个记录,但它只保留我的section表的最后一条记录。
我根据所选的保管箱(部分)获取记录,然后在该下拉列表中,它将获取该表中的内容。
这是我的控制器
public function subjectCreate(){
$users1 = [];
$sections = [];
$users1 = User::where('isTeacher', '1')->where('isAdmin', '0')->lists('firstname','id');
$sections = Section1::lists('name','name');
return View::make('teacher.create_subject', compact('users1','sections')
);
}
public function subjectStore(){
$validate = Validator::make(Input::all(), array(
'name' => 'required|unique:subject'
));
if ($validate->fails()){
return Redirect::route('subjectCreate')->withErrors($validate)->withInput();
} else {
foreach(Section1::where('name', '=', Input::get('section_name'))->get() as $section)
foreach(User::where('id', '=', Input::get('teacher_id'))->get() as $teacher)
$teacher_id = Input::get('teacher_id');
$subject = new Subject1();
$subject->name = Input::get('name');
$subject->student_id = $section->student_id;
$subject->student_firstname = $section->student_firstname;
$subject->student_lastname = $section->student_lastname;
$subject->teacher_id = $teacher_id;
$subject->teacher_firstname = $teacher->firstname;
$subject->teacher_lastname = $teacher->lastname;
$subject->section_id = $section->id;
$subject->section_name = $section->name;
$subject->save();
}
return Redirect::route('viewSubject')->with('success', 'SUBJECT HAS BEEN RECORDED!');
}
以下是我的下拉列表
<div class="form-group">
{{ Form::label('teacher_name', 'Assign a Teacher to this subject:') }}
{{ Form::select('teacher_id', $users1, null) }}
</div>
<div class="form-group">
{{ Form::label('section_name', 'Assign a Section to this subject:') }}
{{ Form::select('section_name', $sections, null) }}
</div>
答案 0 :(得分:0)
看起来你错过了内部foreach
上的花括号。
所以你的代码在这里
foreach(User::where('id', '=', Input::get('teacher_id'))->get() as $teacher)
$teacher_id = Input::get('teacher_id');
$subject = new Subject1();
...
相当于下面的代码段。因此,每个部分只保存1个主题。
foreach(User::where('id', '=', Input::get('teacher_id'))->get() as $teacher) {
$teacher_id = Input::get('teacher_id');
}
$subject = new Subject1();
...
这里应该是else
块中的代码。
foreach(Section1::where('name', '=', Input::get('section_name'))->get() as $section) {
foreach(User::where('id', '=', Input::get('teacher_id'))->get() as $teacher) {
$teacher_id = Input::get('teacher_id');
$subject = new Subject1();
$subject->name = Input::get('name');
$subject->student_id = $section->student_id;
$subject->student_firstname = $section->student_firstname;
$subject->student_lastname = $section->student_lastname;
$subject->teacher_id = $teacher_id;
$subject->teacher_firstname = $teacher->firstname;
$subject->teacher_lastname = $teacher->lastname;
$subject->section_id = $section->id;
$subject->section_name = $section->name;
$subject->save();
}
}