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

unk's avatar
Level 1

trying to update "status" on destroy function

Hello! I have this function in controller

public function destroy($id)
    {
        $appointments = Booking::find($id);
        $appointmentsDelete = $appointments->delete();
        Time::where('appointment_id')->where('time')->update(['status'=>0]);
        return redirect()->route('my.booking')->with('message', 'Booking deleted successufully');
    }

When i delete appointment from table "Booking" i want to update "status" from the table Time to be 0. When i make new appointment and i selected new time status will be 1 and disappear from selectable list. So, I want it to return to the list by updating status 0. Status 0 means hours are available. And 1 when someone makes an appointment, and the time is no longer available. In my case, when i delete appointment, time doesn't return in that list.

0 likes
7 replies
tykus's avatar
Time::where('appointment_id', $id)->where('time', $booking->time) //... maybe???
1 like
unk's avatar
Level 1

@tykus I don't know what parameters to give it...

tykus's avatar

@unk I can’t know them, I can only guess.

What is the relationship between Booking and Time?

1 like
unk's avatar
Level 1

@tykus Maybe help this example

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

In "times" table i have: id, appointment_id, time and status

tykus's avatar
tykus
Best Answer
Level 104

@unk Ok, so you have no relationship between the Appointment and the Booking, so that makes the update more difficult... but you can use $booking->time for the second constraint.

public function destroy($id)
{
    $appointments = Booking::find($id);
    $appointmentsDelete = $appointments->delete();
    Time::where('appointment_id') // how can you get the appointment ID?
        ->where('time', $booking->time)
        ->update(['status'=>0]);
    return redirect()->route('my.booking')->with('message', 'Booking deleted successufully');
   }
1 like
unk's avatar
Level 1

@tykus for the second constraint i give this errror: "Undefined variable: booking" and i defined

$booking = Booking::all();

but i received another error: "Property [time] does not exist on this collection instance"

I don't know how to resolve this situation..I only want when i delete appointment to return back timeslots in selectable list times.

tykus's avatar

@unk you have an individual Booking instance; you called it $appointments - my bad using $booking

$appointments = Booking::find($id);
Time::where('appointment_id') // how can you get the appointment ID?
    ->where('time', $appointments->time)
    ->update(['status'=>0]);
1 like

Please or to participate in this conversation.