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

Time22's avatar

Query appointments from table

am working on system where by you create your appointment time slot its already save in the database i want to retrieve based on date but am having null when i dd the code.

    {
       $date = $request->date;
       $appointment = Appointment::where('date',$date)->where('user_id',auth()->user()->id)->first();
       if(!$appointment){
        return redirect()->to('/appointment')->with('errmessage','Appointment Time not Available for this Date');
       }
       $appointmentId = $appointment->id;
       $times = Time::where('appointment_id',$appointmentId)->get();
       return $times;
       return view('backend.appointment.index',compact('appointmentId','times','date'));

    }```
0 likes
4 replies
tykus's avatar

Your appointment fills an entire day; or a user can have only one appointment per day?

tykus's avatar

@Timene I don't understand how this is supposed to determine if an Appointment is available to the authenticated user

$date = $request->date;
$appointment = Appointment::where('date',$date)->where('user_id',auth()->user()->id)->first();

What data is stored in appointments table; and what is stored in times?

Time22's avatar

@tykus ```$this->validate($request,[ 'date' => 'required|unique:appointments,date,NULL,id,user_id,'.\Auth::id(), 'time' => 'required' ]);

    $appointment = Appointment::create([
     'user_id' => auth()->user()->id,
     'date' => $request->date
    ]);
    foreach ($request->time as $time) {
        Time::create([
           'appointment_id' => $appointment->id,
           'time' => $time,
           'status' => 0,
        ]);
        
    }
    Session::flash('success','Appointment Successfully Created for'.$request->date);
    return redirect()->back();```

i have an appointment table and a time table above is the code for storing both time and appointment

Please or to participate in this conversation.