如何通过laravel使用事件和监听器来处理预订?

时间:2018-01-16 09:55:09

标签: laravel events event-handling database-migration

我在医院有房间。每个房间都有数字床。我想给病人分配病床。我怎样才能以一种简单的方式处理这个问题:

rooms迁移:

Schema::create('rooms', function (Blueprint $table) {
     $table->increments('id');
     $table->string('name');
     $table->integer('floor');
     $table->text('description');
     $table->timestamps();
});

beds迁移:

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(); 
});

reservations迁移:

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();
});

如何查看可用床位?获得可用的床?确保床铺可用。我如何使用事件和听众来使整个事情发挥作用?

1 个答案:

答案 0 :(得分:0)

pluck()检索包含单个列值的Collection。

在这里,它检索预订集合bed_id:

$reserved = Reservation::pluck('bed_id)->all();

whereNotIn方法验证给定列的值不包含在给定数组中:

$bed_available = Bed::whereNotIn('id' , $reserved)->get();

此查询为您提供了所有床位,其ID在该集合中不可用$ reserved,我认为这是您的要求。