【问题标题】:Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an arrayLaravel:preg_replace():参数不匹配,模式是字符串,而替换是数组
【发布时间】:2015-06-19 15:35:16
【问题描述】:

我想将我的结果保存在数据库中,但出现错误异常。

在我看来,我有一个单选按钮(数组),可以获取每个学生的结果 出席、迟到、缺席、其他

这是我的看法

  <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>

这是我的控制器

  $attendance = new Attendances();
  $attendance->status = Input::get('result');
  $attendance->comment = Input::get('comment');
  $attendance->save();

【问题讨论】:

    标签: laravel laravel-4 eloquent laravel-5


    【解决方案1】:

    我有一个类似的错误,使用

    $attendance->fill(array with non-existing columns);
    

    设置一个不存在的列值,以此类推

    $attendance->save();
    

    之后,它会抛出该错误

    【讨论】:

      【解决方案2】:

      由于您选择的命名约定,您的无线电输入 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>
      

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

      控制器代码:

      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();
      }
      

      建议:

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

      查看代码:

      <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>
      

      用于保存的控制器代码

      //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();
      }
      

      【讨论】:

      • 如果我的命名约定是数组,我该如何保存? ://
      • 无线电输入返回单个值。你有什么理由选择'result['.$users-&gt;student_id.']' 作为收音机的名字吗?
      • 我这样做的原因是因为我希望表中的每个学生在记录该部分的出勤时都有不同/相同的结果(例如,学生 1 出席,学生 2 迟到)
      • 为答案添加了解决方案。
      • 好的,等我试试 :)
      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多