【问题标题】:Laravel Relation: Check if a slot is taken or notLaravel 关系:检查一个插槽是否被占用
【发布时间】:2016-02-12 18:04:12
【问题描述】:

我在三个表中设置了关系

平板模型

/**
     * 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);

将只返回与 containerlots 关联的盘子,但仍然不知道哪个盘子在哪个插槽中。

请帮忙。

更新:添加表格

盘子

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案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(); 
    

    【讨论】:

    • 感谢您抽出宝贵时间回答!我已经尝试过您建议的解决方案,但是,它只返回空数组[]。我不知道为什么,虽然我的数据库中有数据。 Collection {#408 #items: [] } 是我使用 dd $freeslots 时得到的。我该如何调试?
    • 您始终可以通过在查询生成器上调用 toSql() 来获取生成的 SQL。执行并粘贴结果:dd($plateContainer->containerSlots()->whereDoesntHave('plate')->toSql());
    • 其实,在你的情况下有更简单的方法,我会在一秒内更新答案
    • 谢谢!但我担心$freeSlots = $plateContainer->containerSlots()->whereNull('plate_id')->get(); 不会这样做,因为containerslots 表中没有plate_id。我已经用表格更新了问题。我也试过dd($plateContainer->containerSlots()->whereDoesntHave('plate')->toSql());。请提供任何其他解决方案。
    • 啊,我认为关系是相反的。无论如何,您能粘贴使用 toSql() 生成的 SQL 吗?它应该可以工作,你应该得到类似 "select * from container_slots where (select count(*) from plates where plates.container_slot_id = container_slots.id)
    猜你喜欢
    • 2018-12-25
    • 2020-06-21
    • 2021-08-13
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多