【问题标题】:Laravel: inserting arrays to a tableLaravel:将数组插入表
【发布时间】:2015-04-14 21:50:17
【问题描述】:

我得到了这段代码,我可以在其中存储每个学生的所有结果(出席、迟到、缺席、其他)。

这是我的看法

 <div align="center"><b>List of Students Enrolled</b></div>
  <table class="table table-striped table-bordered">
    <thead>
        <tr>
   <th>Student ID</th>
    <th>Student Name</th>
    <th>Present</th>
    <th>Late</th>
    <th>Absent</th>
    <th>Others</th>
    <th>Comments</th>

        </tr>
    </thead>

    <tbody>
        @foreach ($users as $users)
            <tr>
      <td>{{ $users->student_id  }} </td>
       <td>{{ $users->student_firstname  }} {{ $users->student_lastname  }}</td> 
    <td>{{ Form::radio('student['.$users->student_id.'][status]', 'present' , true) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'late' ) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'absent') }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'others') }}</td>
<td>{{ Form::text('student['.$users->student_id.'][comment]') }}</td>





          @endforeach

            </tr>
      {{ HTML::script('js/bootstrap.js'); }}

    </tbody>

</table>

这是我保存的控制器

 foreach(Subject1::where('section_name', 'like', Input::get('section_name'))->where('code','like',Input::get('subject_code'))->get() as $student){



  foreach (Input::get('student') as $studentId ) {


                $attendance = new Attendances();
                $attendance->status = $studentId['status'];
                $attendance->comment =  $studentId['comment'];
               $attendance->student_id = $student->student_id;
                $attendance->student_firstname =  $student->student_firstname;
                $attendance->student_lastname =  $student->student_lastname;
                $attendance->teacher_id = Auth::user()->id;
                $attendance->teacher_firstname = Auth::user()->firstname;
                $attendance->teacher_lastname = Auth::user()->lastname;
                $attendance->section_id = $student->section_id;
                $attendance->section_name = Input::get('section_name');
                $attendance->subject_id = $student->id;
                $attendance->subject_code = $student->code;
                $attendance->subject_name = $student->name;
                $attendance->date_recorded = Input::get('date');
                $attendance->save();



}


}

但是,当我在我的数据库中看到该表时,它显示了两条记录,其中 student1 在当天和 student2 当天有出席和缺席(假设我在 student1 上输入出席而在 student2 上输入缺席

请帮忙:(

【问题讨论】:

  • 请用整个表单代码更新问题。

标签: php laravel laravel-4 eloquent laravel-5


【解决方案1】:

您似乎从输入中遍历了几个学生,但还从循环外部合并了另一个学生模型。这可能是您的问题所在。

除此之外,您可能应该只将学生 ID 存储在出勤表上,而不是像名字/姓氏这样的所有学生数据。想想如果有人改名,事情会变得多么糟糕。

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2019-05-09
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多