gpt4 book ai didi

Laravel:preg_replace():参数不匹配,模式是字符串,而替换是数组

转载 作者:行者123 更新时间:2023-12-02 13:50:21 29 4
gpt4 key购买 nike

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

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

这是我的观点

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

这是我的 Controller

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

最佳答案

由于您选择的命名约定,您的 radio 输入结果将返回一个数组。

如果您希望保存输入结果的奇异值,请使用以下格式。

查看代码:

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

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

Controller 代码:

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>

用于保存的 Controller 代码

//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():参数不匹配,模式是字符串,而替换是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620335/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com