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

Smsma's avatar
Level 2

hospital management

I have rooms in hospital each of one has number of beds ( 1 or 2 or 3) I want to reserve bed to certain patient how can I handle this in my project in easy way

rooms migration :

 $table->increments('id');
            $table->string('name');
            $table->integer('floor');
            $table->text('description');
            $table->timestamps();

beds migration

Schema::create('beds', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('quality');
            $table->string('charge');
            $table->integer('room_id')->unsigned();;
            $table->foreign('room_id')->references('id')->on('rooms')
                  ->onUpdate('cascade')->onDelete('cascade');
            $table->boolean('status');
            $table->timestamps(); 
        });

reservation migration

Schema::create('reservations', function (Blueprint $table) {
            $table->increments('id');
            $table->date('date_in');
            $table->date('date_out');
            $table->boolean('status');
            $table->integer('patient_id')->unsigned();
            $table->foreign('patient_id')->references('id')->on('patients');
            $table->integer('bed_id')->unsigned();
            $table->foreign('bed_id')->references('id')->on('beds');
            $table->timestamps();
        });

how can I check available beds , get Available beds and ensure that the bed is booked or not ??

0 likes
2 replies
deGecko's avatar

You could get all the bed ids from the reservations with a date_out bigger than the current date and status = 1, let's say you store them in $occupiedBedIds. You then query beds for all the beds that don't have their ids in $occupiedBedIds and you've got your available beds. But this is a bit messy.

What I would do would be to create another boolean column on the beds table, call it "available" and have it default to 1, then write an event listener for the saved event on the Reservation model to update the bed based on the date_out and status values. This way, to query the available beds, you just do: Bed::whereAvailable(true)->get().

The listener would look something like this:

Reservation::saved(function ($res) {
    $bedAvailable = $res->date_out->isPast() || $res->status == false;
    $res->bed->update(['available' => bedAvailable]);
});

This assumes that the reservation status is changed at some point in time by another process.

1 like
Smsma's avatar
Level 2

can do this with me step by step I want to understand well I want to know what I will write in model and controller and how can I use listener ?@deGecko

Please or to participate in this conversation.