Home > OS >  Edit Data using Array but the result is the same in Laravel
Edit Data using Array but the result is the same in Laravel

Time:12-30

I want to edit data using an array, but when I submit the edits, the results are all the same from below.

Dashboard

When editing each student name, it has a different value, but when submitting, the results are all the same, like this in MySQL and the Web:

PHPMyAdmin

Code:

Controller

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

Blade

<form method="POST" action="{{route('edit.teacher.grade')}}" enctype="multipart/form-data">
                @csrf
{{--                <input type="hidden" name="id_grade[]" value="{{ $userGrades->id_grade }}">--}}
                <div >
                    <input type="submit"  type="button" value="Save">
                    <a href="{{url('teacher/data-grade')}}"  type="button" >Cancel</a>
                </div>

                <table >
                    <tbody>
                    @php $no = 1; @endphp
                    @forelse($subject as $data)
                        <input
                            type="hidden"
                            name="id_grade[]"
                            value="{{$data->id_grade}}"
                        >

                        <tr >
                            <th scope="row" >
                                {{$no  }}
                            </th>
                            <th scope="row" >
                                {{$data->username}}
                            </th>
                            <th scope="row" >
                                {{$data->name}}
                            </th>
                            <th scope="row" >
                                {{$data->min_score}}
                            </th>
                            <th scope="row" >
                                <input
                                    type="number"
                                    min="0"
                                    max="100"
                                    name="quiz[]"
                                    id="quiz"
                                    value="{{$data->quiz}}"
                                    autocomplete="quiz"
                                    
                                    required
                                >
                            </th>
                            <th scope="row" >
                                <input
                                    type="number"
                                    min="0"
                                    max="100"
                                    name="assignment[]"
                                    id="assignment"
                                    value="{{$data->assignment}}"
                                    autocomplete="min_score"
                                    
                                    required
                                >
                            </th>

I didn't screenshot all the blade coding.

How to edit data array according to the data we have edited data.

CodePudding user response:

Looks like your key is id_grade_detail, you should filter by that field:

@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}}"
    >
    ...

And on the server:

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('/');
}
  • Related