【问题标题】:Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, calledGrammar::parameterize(): 参数 #1 ($values) 必须是数组类型,给定字符串,调用
【发布时间】:2021-06-30 10:38:54
【问题描述】:

这是我的控制器,名为“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');
}

这是我的出席。创建要提交的表单。

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

【问题讨论】:

  • 哪一行抛出这个错误?无论如何,我在store函数$students = Student::all();上发现了一个不必要的行

标签: laravel laravel-5 eloquent laravel-4 laravel-8


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2020-10-15
    • 2021-08-19
    • 2021-02-08
    • 2019-08-23
    • 1970-01-01
    • 2021-12-24
    • 2018-10-07
    相关资源
    最近更新 更多