Summer Sale! All accounts are 50% off this week.

hafizewp's avatar

Error Update and Add data in Laravel MySQL

Why is there an error when updating or editing data in Laravel using a data array, like this error.

Trying to access array offset on value of type null

Database in MySQL:

Screenshot my Database in MySQL

Code:

Controller:

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

        foreach($request->all() as $index => $value) {
            GradeDetail::query()->update(array(
                $data['id_grade'] => $value,
                $data['id_student'] => $request->get('id_student')[$index],
                $data['quiz'] =>$request->get('quiz')[$index],
                $data['assignment'] => $request->get('assignment')[$index],
                $data['d_t'] => $request->get('d_t')[$index],
                $data['min_text'] => $request->get('min_text')[$index],
                $data['final_text'] => $request->get('final_text')[$index],
                $data['total'] => $request->get('final_text')[$index],
            ));

        }

        return redirect('teacher/data-grade');
    }

Blade:

<form method="POST" action="{{route('edit.teacher.grade')}}" enctype="multipart/form-data">
                @csrf
                <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_detail[]"
                            value="{{$data->id_grade_detail}}"
                        >

                        <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}}
                                <input
                                    type="hidden"
                                    name="id_student[]"
                                    id="id_student"
                                    value="{{$data->id}}"
                                >
                            </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>

And how or which part of the error to be able to update data using an array?

0 likes
4 replies
kokoshneta's avatar

Where does the error occur? An error message with no context isn’t much use.

Ideally, you should share the Flare error page with us – then we can see exactly what’s going wrong and where.

tykus's avatar

@hafizewp this expression produces null

$data = GradeDetail::find($request->id_grade_detail);

You need to take some steps to mitigate, e.g. use findOrFail instead of find

1 like
tykus's avatar

@hafizewp you are doing nothing to constrain that update query; it must be updating every record in the table?

Please or to participate in this conversation.