Laravel Relation:检查槽是否被占用

时间:2021-11-20 16:54:11

I have relations setup in three tables

我在三个表中设置了关系

Plate model

板模型

/**
     * A plate belongsto an platecontainer
     * @return platecontainer relation
     */
    public function plateContainer()
    {
        return $this->belongsTo('App\Models\PlateContainer');
    }

    /**
     * A plate belongsto slot
     * @return containerslots relations
     */
    public function containerSlots()
    {
      return $this->belongsTo('App\Models\ContainerSlot');
    }

PlateContainer model

PlateContainer模型

  public function plates()
    {
        return $this->hasMany('App\Models\Plate');
    }

    /**
     * A plate Container has many slots in it.
     * @return containerslot model relation
     */
    public function containerSlots()
    {
      return $this->hasMany('App\Models\ContainerSlot');
    }

And finally ContainerSlot model

最后是ContainerSlot模型

/**
     * A containerslot belongsTo plateContainer
     * @return platecontainer relation
     */
    public function plateContainer()
    {
      return $this->belongsTo('App\Models\PlateContainer');
    }

    /**
     * A containerslot has one plate
     * @return hasOne relation with plate model
     */
    public function plate()
    {
        return $this->hasOne('App\Models\Plate');
    }

Upon selection of an platecontainer, the user gets to see all the slots which are available and which are taken. How I might achieve this??

在选择了一个平板容器后,用户可以看到所有可用的插槽和哪些插槽。我怎么可能实现这个?

Here is my containerslot table

这是我的容器表

  public function up()
    {
        Schema::create('container_slots', function (Blueprint $table) {
            $table->increments('id');
            $table->tinyInteger('slots')->default(15);
            $table->boolean('taken');
            $table->integer('plate_container_id')->unsigned()->index();

            $table->foreign('plate_container_id')->references('id')->on('plate_containers');

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

Something like this

像这样的东西

return   $p = \App\Models\PlateContainer::with('containerSlots.plate')->find(21);

will return only the plates associated with containerslots but still don't know which plate in which slot.

将只返回与容器槽相关的板,但仍然不知道哪个槽位于哪个槽中。

Please help.

请帮忙。

UPDATE: added tables

更新:添加表格

Plates table

盘子表

public function up()
{
    Schema::create('plates', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('serial_number');
        $table->string('crc-code', 50);
        $table->string('reason', 50)->nullable();
        //$table->tinyInteger('plate_container_slot')->nullable();
        $table->integer('stack_id')->nullable()->unsigned()->index();
        $table->integer('plate_container_id')->nullable()->unsigned()->index();
        $table->integer('container_slot_id')->nullable()->unsigned()->index();

        $table->foreign('stack_id')->references('id')->on('stacks')->onDelete('cascade')->onUpdate('cascade');
        $table->foreign('plate_container_id')->references('id')->on('plate_containers')->onDelete('cascade');
        $table->foreign('container_slot_id')->references('id')->on('container_slots')->onDelete('cascade');

        $table->softDeletes();

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

platecontainer tables

板材容器表

public function up()
{
    Schema::create('plate_containers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 25)->unique();
        //$table->tinyInteger('number_of_slots');
        $table->text('description')->nullable();
        $table->string('material', 50);


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

containerslots table

containerslots表

public function up()
{
    Schema::create('container_slots', function (Blueprint $table) {
        $table->increments('id');
        $table->tinyInteger('slots')->default(15);
        $table->boolean('taken');
        $table->integer('plate_container_id')->unsigned()->index();

        $table->foreign('plate_container_id')->references('id')->on('plate_containers');

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

1 个解决方案

#1


1  

You can use Eloquent's whereDoesntHave query method to determine which container slots for given plate container have no related plates.

您可以使用Eloquent的whereDoesntHave查询方法来确定给定板容器的哪些容器槽没有相关的板。

// select a container
$plateContainer = PlateContainer::find($id); 

// all slots that belong to given container that do not have a plate assigned
$freeSlots = $plateContainer->containerSlots()->whereDoesntHave('plate')->get(); 

#1


1  

You can use Eloquent's whereDoesntHave query method to determine which container slots for given plate container have no related plates.

您可以使用Eloquent的whereDoesntHave查询方法来确定给定板容器的哪些容器槽没有相关的板。

// select a container
$plateContainer = PlateContainer::find($id); 

// all slots that belong to given container that do not have a plate assigned
$freeSlots = $plateContainer->containerSlots()->whereDoesntHave('plate')->get();