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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29620335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:57:41  来源:igfitidea点击:

Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

laravellaravel-4eloquentlaravel-5

提问by Virgil Joseph Cruz

I want to save my results in the database but im getting an error exception.

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

In my view I have a radio button(array) that gets the result of each student which is present,late,absent,others

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

Here's my view

这是我的看法

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

Here's my controller

这是我的控制器

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

回答by Mysteryos

Your radio input resultwill return an array, due to the naming convention you have chosen.

result由于您选择的命名约定,您的收音机输入将返回一个数组。

If you wish to save the singular value of the input result, use the following format.

如果要保存 input 的奇异值result,请使用以下格式。

View code:

查看代码:

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

If you are expecting multiple values in an array, then you should be looping through the code, and saving each individually:

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

Controller Code:

控制器代码:

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

Suggestion:

建议:

If you wish to save several student's information on the same form, the suggestion below will work great.

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

View Code:

查看代码:

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

Controller Code for saving

用于保存的控制器代码

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

回答by cnlevy

I had a similar error, where using

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

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

sets a non-existing column value, so on calling

设置一个不存在的列值,所以调用

$attendance->save();

afterwards, it will throw that error

之后,它会抛出那个错误