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

unk's avatar
Level 1

I want to use DB:: on function destroy() to delete

Hello! I want to use DB to delete an appointment from my database and returning time slots back when I delete this appointment. My tables: bookings table https://imgur.com/Rtkk1ZG times table https://imgur.com/qNRNijw So, i want to delete this booking from my row and I want to update status from times table to be 0 not 1 when I delete this booking.. to return time back to selectable list again. https://imgur.com/cnJKarm - This time from booking i want to return back to selectable list when I delete.

0 likes
10 replies
tykus's avatar

Why do you need a Time model at all? Why can't you compute the available times from the Bookings?

1 like
unk's avatar
Level 1

@tykus And how to update time from bookings? I want to be status 0 not 1 when I delete this booking.

tykus's avatar

@unk is appointment_id on the times table a foreign key for the Booking?

1 like
tykus's avatar

@unk I am struggling to understand how you have bookings, appointment and times tables to represent the same concept. Can you show how these entities are related; can you provide an ERD?

When a Booking is made; how is a Time status changed; can you show the relevant code (Booking creation and Time update)?

1 like
unk's avatar
Level 1

@tykus

  public function store(Request $request)
    {
        date_default_timezone_set('Europe/Bucharest');
        $request->validate(['time'=>'required']);
        $check = $this->checkBookingTimeInterval();
        if($check){
            return redirect()->back()->with('errmessage', 'You already made an appointment. Please wait to make next appointment');
        }
        Booking::create([
            'user_id'=>auth()->user()->id,
            'doctor_id'=> $request->doctorId,
            'date'=> $request->date,
            'time' => $request->time,
            'status' =>0
        ]);
        Time::where('appointment_id', $request->appointmentId)
            ->where('time', $request->time)
            ->update(['status'=>1]);
        //Send email notification
        $doctorName = User::where('id', $request->doctorId)->first();
        $mailData = [
            'name'=>auth()->user()->name,
            'time'=> $request->time,
            'date'=> $request->date,
            'doctorName' => $doctorName->name
        ];
        try{
            \Mail::to(auth()->user()->email)->send(new AppointmentMail($mailData));
        }catch(\Exception $e){

        }
        return redirect()->back()->with('message', 'Your appointment was booked');
    }
kokoshneta's avatar

@unk Why on earth are you using varchar(255) for your date and time columns? And why are they separate? Why aren’t they just datetime or timestamp columns?

And as @tykus said, why do you have a separate times table at all? What is the difference between an appointment and a time? If Appointment objects represent appointments patients have made with their doctor, what do Time objects represent in the real world? Are they time slots that are available (or unavailable) for booking appointments? So for example a Time object could represent 9:00–9:15 AM?

1 like
unk's avatar
Level 1

@kokoshneta Time is used for timeslots when doctors make their bookings for patients (1pm, 2pm, 3pm, 4pm, 5pm...). Appointment table has dates whe doctors make their appointments and Time table is for the hours for that date.

kokoshneta's avatar

@unk In that case, your  appointments table should be storing the date (as a date type column) and then a reference to the time slot ID as a foreign key. The times table should not have a reference to appointments, and there’s no need for a status column either – just fetch all time slots and check if there are any appointments for each day + time slot combination. If there are, it’s unavailable, otherwise it’s available.

1 like
cassey's avatar

@unk May I know how do you solve the problem at last? Please send the complete solution..thank you!

Please or to participate in this conversation.