skip to Main Content

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

foreach() argument must be of type array|object, string given

Database in MySQL:

Screenshot my Database in MySQL

Code:

Controller:

public function UpdateGradeStudent(Request $request){

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

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

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

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 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>
                    @forelse($subject as $data)
                        <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">
                                <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>

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

2

Answers


  1. You have to change id_grade hidden input like this:

    <input type="hidden" name="id_grade[]" value="{{ $userGrades->id_grade }}">
    
    Login or Signup to reply.
  2. $request->get('id_grade') will return the value of id_grade which will be a string as mentioned by @zohrehda. Please update your foreach loop to be

    foreach($request->all() as $index => $value) {

    $request->all() returns an array of all your inputs which can be iterated upon.

    Additionally, your update method has a typo. It should be GradeDetail::update and not GradeDetail::updated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search