我是PHP和Laravel的新手,在通过PHP post方法返回/获取所有学生对象时遇到了问题。我想做的是,当单击“保存”按钮时,将保存所有对每个学生都存在/不存在的值。现在,它只返回我所有记录的最后一个学生并保存。也就是说,如果有100名学生,则会保存上次学生出勤率的100倍。这是我的代码:
<form action="store_attendence_student" method="POST">
<div class="form-group">
<label class="control-label">Date:</label>
<input name="attendecnedate" type="date" class="form-control">
</div>
@foreach($stdcourse1 as $d)
<h3> {{$d->student_registration_num}}</h3>
<h3> {{$d->student_name}}</h3>
<input type="hidden" name="s_id" value="{{$d->student_registration_num}}" >
<input type="hidden" name="s_name" value="{{$d->student_name}}" >
<select name="atten">
<option value="present">Present</option>
<option value="absent">Absent</option>
</select>
@endforeach
<div class="form-layout-footer" style="margin-left: 40%">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-success mg-r-5"> Save</button></a>
</div>
</form>
这是控制器的保存操作:
public function store_attendence_student(Request $request)
{
$data7=teachers::select('teacher_id')->where('teacher_email','=',Session::get('teacher_username'))->pluck('teacher_id')->first();
$course1= teachers::select('course_id')->where('teacher_id','=',$data7)->pluck('course_id')->first();
$stdcourse1=student::leftjoin('courses','courses.sem_id','=','students.sem_id')->where('courses.id','=',$course1)
->select('students.student_name','students.student_id')->count();
for ($i=0; $i < $stdcourse1 ; $i++)
{
$attendence = new attendence();
$attendence->student_id=$request->s_id;
$attendence->student_name=$request->s_name;
$attendence->course_id=$course1;
$attendence->teacher_id=$data7;
$attendence->date=$request->attendecnedate;
$attendence->status_attendence=$request->atten;
$attendence->save();
}
}
答案 0 :(得分:0)
尝试一下:
for ($i=0; $i < $stdcourse1 ; $i++)
{
$attendence = new attendence();
$attendence->student_id=isset($request[$i]->s_id)? $request[$i]->s_id : '0';
$attendence->student_name=isset($request[$i]->s_name) ? $request[$i]->s_name : '';
$attendence->course_id=isset($course1[$i]) ? $course1[$i] : '';
$attendence->teacher_id=isset($data7[$i]) ? $data7[$i] : '';
$attendence->date=isset($request[$i]->attendecnedate) ? $request[$i]->attendecnedate : '2018-12-20 07:00:00'; **//change this static value to dynamic one**
$attendence->status_attendence=isset($request[$i]->atten) ? $request[$i]->atten : '';
$attendence->save();
}