ctyler
193
16
Laravel

Issue adding days in a for loop

Posted 1 month ago by ctyler

Hello all, I am working on a training application. Trainers will need to take attendance for a course. So when generating course(s), admins can select: Start date, number of courses, Interval between courses, Number of session, Interval between sessions.

So each course will have an attendance object. Each attendance object will have one or more sessions. The problem I am having is the sessions and the dates.

$validated = $request->validated()
$no_courses = 0;
$courseDate = new Carbon($validated['start_date']); //Comes from the form
//The course, for loop,  works great
for($i = 0; $i < $validated['no_classes'];$i++) {
                $course = Course::create([
                    'course_cat_id' => $coursecat->id,
                    'name' => $coursecat->name . ' : ' . $courseDate->format('Y-m-d'),
                    'start_date' => $courseDate->format('Y-m-d'),
                    'start_time' => $validated['start_time'],
                    'end_time' => $validated['end_time'],
                    'summary' => $validated['description'],
                ]);
                $courseDate->add($validated['interval'], 'day');
                $no_courses++;
                $attendance = Attendance::create([ //One attendance object per course. this works
                    'course_id' => $course->id,
                ]);
//So the number of session_date is the problem. Course can span multiple days and attendance will need to be taken every day (session).
                for($a = 0; $a < $validated['attendance_sessions'];$a++) {
                    $sesssionDate = $courseDate;
                   Attendancesession::create([
                        'attendances_id'    => $attendance->id,
                        'session_starttime' => $validated['start_time'],
                        'session_endtime' => $validated['end_time'],
                        'session_date' => $sesssionDate,
                    ]);
                    $sesssionDate = $sesssionDate->addDays($validated['session_interval']); //This is not working in this for loop. The number of 'session_interval' in days should be added each iteration. 
                }
    }

So once this runs the Courses are created with the correct number of days between each course. The sessions, on the other hand, all have the same date which is the first course date when they are supposed to start on the first course created date and the session_interval between the rest of the sessions.

I hope I explained it clearly.

Any assistance would be appreciated.

Please sign in or create an account to participate in this conversation.