Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AO's avatar
Level 1

prevent students from registration

Hello

I have a small system which students can register for courses

the problem when someone is registered on a course, he can register in any course that has the same date lets say.

I have a date start and a date end for a course.

I register users using :


public function register($student, $course)
    {
       
        CourseRegister::create([
            'student'=>$student,
            'course'=>$course
            ]);
      
        
        return redirect()->back();
    }

How I can prevent students from registering in courses that have the same date?

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You can add a validation to check if the User is already registered on another course on that date:

public function register($student, $course)
{
    $overlap = CourseRegister::where('student', $student->id)
        ->where('end', '>', $course->start)
        ->where('start', '<',  $course->end)
        ->exists();

    if ($overlap) {
        // cannot register
    }

    CourseRegister::create([
        'student'=>$student,
        'course'=>$course
    ]);
      
        
    return redirect()->back();
}

*** Using student and course as foreign key columns goes against the convention of having a _id suffix; you might find naming the relations awkward as a result...

2 likes

Please or to participate in this conversation.