【问题标题】:Laravel: how to insert an array with radio button per rowLaravel:如何插入每行带有单选按钮的数组
【发布时间】:2015-04-15 08:54:22
【问题描述】:

我想在我的状态所在的每一行为每个学生插入不同的状态(出席、缺席、迟到、其他

这是我的看法

<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

无论如何我可以在我的控制器的一个函数中插入它吗?

谢谢!

【问题讨论】:

  • 请清楚你要做什么
  • 我想在我的表中插入每行每个学生的不同状态假设 student1 存在,student2 不存在......使用单选按钮作为每个学生的数组
  • 为什么要名称为数组?您可以使用字符串连接创建不同的名称值:'student'.$users-&gt;student_id
  • uhm.... 不,实际上,我想要实现的是获得每个学生的结果(例如在场、缺席、迟到、其他),我指的是每一行都是学生 ID

标签: laravel laravel-4 eloquent laravel-5


【解决方案1】:

您的控制器中的Input::get('student')[1]['status']; 应该返回第一个学生的状态,其他学生的状态也是如此。

foreach( Input::get('student') as $key => $student ) {
  if( array_key_exists( 'status', $student )) {
     $s = Student::find( $key );
     $s->status = $student['status'];
     $s->save()
  }
}

应该将每个学生的状态保存到表中。

【讨论】:

  • Student::find( $key )-&gt;status = $student['status']; 只为student 对象赋值。您需要在对象上调用save()
  • 嗨,我试过这样:foreach (Input::get('student')as $key => $value) { if(array_key_exists('status', $value)) { $s = 用户::find($key)->status = $value['status']; $出席=新出席(); $出席->状态= $s; $出席->保存(); } } 它仍然给我错误的记录:c 我错过了什么吗?还是我输入错误? :(
  • 我在声明 Input::get('student')[1]['status']; 时有一个未定义的偏移量
  • 您在哪里保存状态,在用户模型或出勤模型中?如果您将其保存到出勤模型中,请留下User::find(...) 并简单地留下$attendance = new Attendance; $attendance-&gt;status = $value['status']; $attendance-&gt;user_id = $key
猜你喜欢
  • 2023-03-12
  • 2019-04-04
  • 2017-12-25
  • 2017-03-30
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
相关资源
最近更新 更多