Home > Software engineering >  Error Update and Add data in Laravel MySQL
Error Update and Add data in Laravel MySQL

Time:12-16

Want to ask earlier, I did insert add data in the attendance_details table, it worked, but I want the attendance table to be automatically changed to the Done table to 1 before 0 before submitting. But the error at signup says something like this Method Illuminate\Database\Eloquent\Collection::save does not exist. Here is my MySQL table:

Table attendance_details: Screenshot Table attendance_details in MySQL Screenshot Table attendance

in the attendance table there is a Done table still 0 after submitting it should be changed to 1

Code:

Blade

 <form method="POST" action="{{route('add.data')}}" enctype="multipart/form-data">
        @csrf
        <input type="hidden" name="id_atte" value="{{ $dataAttendance->id_atte }}"
>

Controller

public function ViewData($id){
        $dataAttendance = Attendance::findOrFail($id);
        
        $dataAttendanceTa = Attendance::join('subjects', 'attendances.id_subject', '=', 'subjects.id_sub')
            ->join('class_infos', 'subjects.id_class', '=', 'class_infos.id')
            ->join('class_details', 'class_infos.id', '=', 'class_details.id_class')
            ->join('users', 'class_details.id_user', '=', 'users.id')
            ->where('attendances.id_atte', '=', $id)
            ->get();

        return view('index', compact('dataAttendanceDe', 'dataAttendance', 'dataAttendanceTa'));
    }




public function AddData(Request $request) {
        // Add Data attendance_details
        foreach($request->get('id_atte') as $index => $value) {
            AttendanceDetail::create([
                'id_atte' => $value,
                'id_student' => $request->get('id_student')[$index],
                'id_atte_type' =>$request->get('id_atte_type')[$index],
                'note' => $request->get('note')[$index]
            ]);
        }

        // Update Data attendance
        $data = Attendance::find($request->id_atte);
        $data->done = '0';
        $data->save();

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

Where is the coding wrong?

CodePudding user response:

From the context of your code $request->id_atte returns an array of id's which you loop over. This means that $data = Attendance::find($request->id_atte); will return an array of models instead of just one, which means it cannot be saved.

You could either set the done attribute in another foreach loop or within the origional creation.

CodePudding user response:

inside the loop function, you should update the Attendance model

    public function AddData(Request $request) 
    {
            // Add Data attendance_details
            foreach($request->get('id_atte') as $index => $value) {
                AttendanceDetail::create([
                    'id_atte' => $value,
                    'id_student' => $request->get('id_student')[$index],
                    'id_atte_type' =>$request->get('id_atte_type')[$index],
                    'note' => $request->get('note')[$index]
                ]);
               // Update Data attendance
               $data = Attendance::find($value);
               $data->done = '0';
               $data->save();
            }
    
            return redirect('teacher/data-attendance');
        }

OR you can use the update query method after whereIn

    
    // Update Data attendance
    $data = Attendance::whereIn('id', $request->id_atte)->update([
        'done' => '0'
    ]);

  • Related