Laravel Relation:检查槽是否被占用

时间:2015-11-11 15:36:27

标签: php laravel laravel-5

我在三个表中设置了关系

模型

/**
     * 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 模型

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

最后 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');
    }

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

这是我的容器时间表

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

像这样的东西

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

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

请帮忙。

更新:添加了表格

Plates

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

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

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 个答案:

答案 0 :(得分:1)

您可以使用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();