Laravel:preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组

Vir*_*ruz 4 laravel eloquent laravel-4 laravel-5

我想将我的结果保存在数据库中,但我得到一个错误异常.

在我看来,我有一个单选按钮(数组),可以获得每个学生的成绩,包括现在,迟到,缺席,其他人

这是我的看法

  <td>{{ $users->student_id  }} </td>
  <td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td> 
  <td>{{ Form::radio('result['.$users->student_id.']', 'present' , true) }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'late' ) }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'absent') }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'others') }}</td>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

  $attendance = new Attendances();
  $attendance->status = Input::get('result');
  $attendance->comment = Input::get('comment');
  $attendance->save();
Run Code Online (Sandbox Code Playgroud)

Mys*_*yos 13

result由于您选择的命名约定,您的无线电输入将返回一个数组.

如果要保存输入的奇异值result,请使用以下格式.

查看代码:

<td>{{ Form::radio('result', 'present' , true) }}</td>
<td>{{ Form::radio('result', 'late') }}</td>
<td>{{ Form::radio('result', 'absent')   }}</td>
<td>{{ Form::radio('result', 'others')  }}</td>
Run Code Online (Sandbox Code Playgroud)

如果您期望数组中有多个值,那么您应该循环遍历代码,并单独保存每个值:

控制器代码:

foreach (Input::get('result') as $studentId=>$value)
{
     $attendance = new Attendances();
     $attendance->status = $value;
     $attendance->comment = Input::get('comment');
     //We should save the student id somewhere.
     $attendance->student_id = $studentId;
     $attendance->save();
}
Run Code Online (Sandbox Code Playgroud)

建议:

如果您希望在同一表格上保存多个学生的信息,下面的建议将很有效.

查看代码:

<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>
Run Code Online (Sandbox Code Playgroud)

控制器代码用于保存

//We loop through each student and save their attendance.
foreach (Input::get('student') as $studentId=>$data)
{
     $attendance = new Attendances();
     $attendance->status = $data['status'];
     $attendance->comment = $data['comment'];
     //We should save the student id somewhere.
     $attendance->student_id = $studentId;
     $attendance->save();
}
Run Code Online (Sandbox Code Playgroud)