samedi 20 octobre 2018

How to scope a Laravel model with a calculated criteria of available vs booked capacity?

I have Shifts model [id, people_needed, ...]

and Bookings Model [id, shift_id, person_id, confirmed, ...]

I want to list shifts with open positions. i.e, shifts with confirmed bookings < shift.people_needed, preferably using scopeOpen.

public function scopeOpen ($query) {
  return $query->whereDoesntHave('bookings', function ($q) {
   $q->.....
  })
}

Any ideas?

Edit

Database

    Schema::create('shifts', function (Blueprint $table) {
        $table->string('id');
        $table->primary('id');
        $table->unsignedInteger('client_id')->index();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->unsignedInteger('facility_id')->index();
        $table->foreign('facility_id')->references('id')->on('facilities');
        $table->string('qualification');
        $table->tinyInteger('quantity')->unsigned()->default(1);
        $table->unsignedInteger('rate');
        $table->dateTime('start');
        $table->dateTime('end');
        $table->smallInteger('break')->unsigned()->nullable()->comment('in minutes');
        $table->text('description')->nullable();
        $table->timestamps();
        $table->softDeletes();
    });


    Schema::create('bookings', function (Blueprint $table) {
        $table->increments('id');

        $table->unsignedInteger('nurse_id')->index();
        $table->foreign('nurse_id')->references('id')->on('nurses');

        $table->string('shift_id')->index();
        $table->foreign('shift_id')->references('id')->on('shifts');

        $table->string('creator_type')->nullable();
        $table->integer('creator_id')->nullable();

        $table->dateTime('confirmed_at')->nullable();
        $table->string('confirmer_type')->nullable();
        $table->integer('confirmer_id')->nullable();

        $table->dateTime('canceled_at')->nullable();
        $table->string('canceler_type')->nullable();
        $table->integer('canceler_id')->nullable();

        $table->timestamps();
        $table->softDeletes();
    });

What I want is to list available shifts. i.e shifts with fewer bookings that shift->quantity. (other constrains are not important now).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire