Laravel:preg_replace():参数不匹配,pattern是一个字符串,而替换是一个数组 [英] Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
问题描述
在我看来,我有一个单选按钮(数组),得到的结果是每个学生现在,迟到,缺席,其他
这是我的观点
< 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();
您的收音机输入 / code>将返回一个数组,由于您选择的命名约定。
如果要保存输入的奇异值 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');
//我们应该将学生ID保存在某个地方。
$ attendance-> student_id = $ studentId;
$ attendance-> save();
}
建议:
如果您希望在同一个表单上保存几个学生的信息,下面的建议将会很好。
查看代码: / strong>
< 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>
要保存的控制器代码
<$
foreach(Input :: get('student')as $ studentId => $ data)
{
$ attendance = new Attendances();
$ attendance-> status = $ data ['status'];
$ attendance-> comment = $ data ['comment'];
/ / b
$ b $ c $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>
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();
Your radio input result
will return an array, due to the naming convention you have chosen.
If you wish to save the singular value of the input result
, use the following format.
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();
}
这篇关于Laravel:preg_replace():参数不匹配,pattern是一个字符串,而替换是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!