【发布时间】: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();
});
}
【问题讨论】: