Grammar::parameterize(): 参数 #1 ($values) 必须是数组类型,字符串给定,调用

Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called

这是我的名为“AttendanceController”的控制器。

public function index()
{
    $attendances = Attendance::orderBy('date', 'desc')->paginate(50);
    $students = Student::all();
    return view('attendances.index', compact('attendances', 'students'));
}
public function create()
{
    $students = Student::all();
    return view('attendances.create', compact('students'));
}
public function store(Request $request)
{
    $students = Student::all();
    $request->validate(array(
        'attendance' => 'required',
        'date' => 'required',
    ));

    $attendance = Attendance::create(array(
        'attendance' => $request->input('attendance'),
        'date' => $request->input('date'),
    ));

    return redirect()->route('attendances.index')->withSuccess('Done');
}

这是我的 attendances.create 那里有表格要提交。

<form method="post" action="{{route('attendances.store')}}">
        @csrf
        <table>

                <tr>
                    <th><p style="margin: 20px">Name : </p></th>
                    <th><p style="margin: 20px">Attendance : </p></th>
                    <th><p style="margin: 20px">Date : </p></th>
                </tr>
            @foreach ($students as $student)
                <tr>
                    <th><p style="margin: 20px">{{ $student->name }}</p></th>
                    <input name="id[]" type="hidden" value="{{ $student->id }}">
                    <th><p style="margin: 20px">
                        <select name="attendance[]" id="attendance">
                            <option value="present">present</option>
                            <option value="absent">absent</option>
                            <option value="half_day">half day</option>
                        </select></p>
                    </th>
                    <th><p style="margin: 20px"><input type="date" class="form-control" name="date[]"></p></th>
                </tr>

            @endforeach
            <td><button class="btn btn-primary" name="submit" id="submit">Submit</button></td>
        </table>
    </form>

您输入的名称导致冲突。您为多个学生发送了表单字段,但没有识别出这些字段。

我没有检查代码,但我希望你能明白。

<form method="post" action="{{route('attendances.store')}}">
        @csrf
        <table>

                <tr>
                    <th><p style="margin: 20px">Name : </p></th>
                    <th><p style="margin: 20px">Attendance : </p></th>
                    <th><p style="margin: 20px">Date : </p></th>
                </tr>
            @foreach ($students as $key => $student)
                <tr>
                    <th><p style="margin: 20px">{{ $student->name }}</p></th>
                    <input name="student[{{$key}}][id]" type="hidden" value="{{ $student->id }}">
                    <th><p style="margin: 20px">
                        <select name="student[{{$key}}][attendance]" id="attendance">
                            <option value="present">present</option>
                            <option value="absent">absent</option>
                            <option value="half_day">half day</option>
                        </select></p>
                    </th>
                    <th><p style="margin: 20px"><input type="date" name="student[{{$key}}][date]" class="form-control"></p></th>
                </tr>

            @endforeach
            <td><button class="btn btn-primary" name="submit" id="submit">Submit</button></td>
        </table>
    </form>

以及您的存储方法:

public function store(Request $request)
{
    $request->validate(array(
        'student' => 'array|required', // laravel array validaton.
        'student.*.id' => 'required', // array item validator with asterisk: https://laravel.com/docs/8.x/validation#validating-arrays
        'student.*.attendance' => 'required',
        'student.*.date' => 'required',
    ));

   foreach($request->get('student') as $student)

      $attendance = Attendance::create(array(
          'student_id' => $student['id'], // if you use in there.
          'attendance' => $student['attendance'],
          'date' => $student['date'],
      ));

   }

    return redirect()->route('attendances.index')->withSuccess('Done');
}