php 使用Array编辑数据,但结果与Laravel相同

aurhwmvo  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(92)

我想使用数组编辑数据,但是当我提交编辑时,下面的结果都是一样的。

当编辑每个学生的名字时,它有一个不同的值,但当提交时,结果都是一样的,就像在MySQL和Web中一样:

代码:
主计长

public function UpdateEditGradeStudent(Request $request){
    $data = GradeDetail::find($request->id_grade_detail);

    foreach($request->id_grade as $index => $value) {
        DB::table('grade_details')
            ->update(array(
                'id_grade' => $value,
                'quiz' => $request->quiz[$index],
                'assignment' => $request->assignment[$index],
                'min_text' => $request->min_text[$index],
                'd_t' => $request->d_t[$index],
                'final_text' => $request->final_text[$index],
                'total' => $request->total[$index],
            ));
        
    }

    return redirect('/');
}

"刀锋"

<form method="POST" action="{{route('edit.teacher.grade')}}" enctype="multipart/form-data">
                @csrf
{{--                <input type="hidden" name="id_grade[]" value="{{ $userGrades->id_grade }}">--}}
                <div class="flex">
                    <input type="submit" class="focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 mt-3 mr-4" type="button" value="Save">
                    <a href="{{url('teacher/data-grade')}}" class="focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 mt-3" type="button" >Cancel</a>
                </div>

                <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
                    <tbody>
                    @php $no = 1; @endphp
                    @forelse($subject as $data)
                        <input
                            type="hidden"
                            name="id_grade[]"
                            value="{{$data->id_grade}}"
                        >

                        <tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
                            <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{$no++}}
                            </th>
                            <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{$data->username}}
                            </th>
                            <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{$data->name}}
                            </th>
                            <th scope="row" class="py-4 px-6 font-medium text-red-700 whitespace-nowrap dark:text-white">
                                {{$data->min_score}}
                            </th>
                            <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                <input
                                    type="number"
                                    min="0"
                                    max="100"
                                    name="quiz[]"
                                    id="quiz"
                                    value="{{$data->quiz}}"
                                    autocomplete="quiz"
                                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
                                    required
                                >
                            </th>
                            <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                <input
                                    type="number"
                                    min="0"
                                    max="100"
                                    name="assignment[]"
                                    id="assignment"
                                    value="{{$data->assignment}}"
                                    autocomplete="min_score"
                                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
                                    required
                                >
                            </th>

我没有截图所有的***刀片***编码。
如何编辑***数据数组***根据我们已经编辑过的数据。

zujrkrfu

zujrkrfu1#

您的键似乎是id_grade_detail,您应该按该字段进行筛选:

@forelse($subject as $data)
    <input
        type="hidden"
        name="id_grade_detail[]"
        value="{{$data->id_grade_detail}}"
    >
    <input
        type="hidden"
        name="id_grade[]"
        value="{{$data->id_grade}}"
    >
    ...

在服务器上:

public function UpdateEditGradeStudent(Request $request)
{
    foreach($request->id_grade_detail as $index => $value) {
        $grade_detail = GradeDetail::find($value);
        if ($grade_detail) {
            $grade_detail->quiz = $request->quiz[$index];
            $grade_detail->assignment = $request->assignment[$index];
            $grade_detail->min_text = $request->min_text[$index];
            $grade_detail->d_t = $request->quiz[$index];
            $grade_detail->final_text = $request->final_text[$index];
            $grade_detail->total = $request->total[$index];
            
            $grade_detail->save();
        }
    }

    return redirect('/');
}

相关问题