0

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

and table attendance: 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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'
    ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.