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

Mawunyo's avatar

Check if appointment date already exists

Hi I want to check if request date is between Appointment start and appointment end date and return error else create appointment. This is my code but it doesn't work.

How can do that

// Check if appointment date already exists

    $exisAppoint = DB::table('appointments')->where('start', $request['start'])
                                            ->where('doctor_id', auth('doctor')->user()->id)                                               
                                            ->first();

    if (($request['start'] >= $exisAppoint->start) && ($request['start'] <= $exisAppoint->end))
        {
            return "This date is already exist";
        }else{

            $appointment =  Appointment::create([
                'patient_id' => $request['patient_id'],
                'doctor_id' => $doctor_id,
                'start' => $request['start'],
                'end' => $appointment_end,
                'title' => $title,
                'class'=> $class,
                'content'=> $content,
                'notes' => $request['notes'],
            ]); 

        
        return  $appointment;

        }
0 likes
10 replies
Nakov's avatar

@mawunyo your check is wrong because you are checking for when the start is the exact time first.. Try this:

$appointmentExists = Appointment::where('doctor_id', auth('doctor')->user()->id)
    ->where('start', '>=', $request['start'])->where('end', '<=', $request['start'])->exists();

if ($appointmentExists)
{
    return "This date is already exist";
}
else
{
    $appointment =  Appointment::create([
        'patient_id' => $request['patient_id'],
        'doctor_id' => $doctor_id,
        'start' => $request['start'],
        'end' => $appointment_end,
        'title' => $title,
        'class'=> $class,
        'content'=> $content,
        'notes' => $request['notes'],
     ]); 

    return  $appointment;
}
Nakov's avatar

@mawunyo can you please share what is contained in $request['start'] and what value you have in your start and end fields in your database?

Mawunyo's avatar

@nakov

$request['start'] : 2019-12-18 08:00:00

In my database for example :

start : 2019-12-18 08:00:00

end : 2019-12-18 08:20:00

So $request['start'] must not be the same like start

Nakov's avatar

@mawunyo so then just this:

$appointmentExists = Appointment::where('doctor_id', auth('doctor')->user()->id)
    ->where('start', $request['start'])->exists();
willvincent's avatar

@nakov your first solution was better. Based on the original post, the task is to determine if the new request overlaps an existing one, so checking whether the requested start date is same or greater than any existing start AND same or less than any existing end will find overlaps.

Otherwise it's just checking for identical starts, which is likely to lead to double bookings -- even if it satisfies current rules.

The issue with your previous example was the >= and <= were reversed

Nakov's avatar

@willvincent yup, I think so too, but @mawunyo said that it doesn't work, so based on this

So $request['start'] must not be the same like start

that he said, I gave him the second answer :)

Snapey's avatar
Snapey
Best Answer
Level 122

if the appointment duration is variable, then your logic needs to be

Collision if;

  • start time of new appointment is between start and end of any record
  • or, end time of new appointment is between start and end of any record
  • or, start time of new appointment is before start time of a record AND end time of new appointment is after end time of any record

This previous answer might help. https://laracasts.com/discuss/channels/laravel/check-if-leave-application-exists-within-dates

Please or to participate in this conversation.