我需要向MySql添加三个表,但是我不知道如何组织它们。
我的桌子是:
与我的关系:
在公寓之后进入了表面,所以我不能将地面用作我的公寓地板。
目前,我的公寓桌子和地面桌子都带有一个枢轴桌子(很多到很多)。
在Mysql中,处理此问题的最佳方法是什么?在laravel里面之后?
感谢您的帮助!
编辑:到目前为止,我在laravel上的工作(仅地面桌和公寓桌)
在Apartment.php中:
public function floors(){
return $this->belongsToMany('App\Models\Copro\Floor');
}
在Floor.php中:
public function apartments()
{
return $this->belongsToMany('App\Models\Copro\Apartment');
}
编辑2:我到目前为止的迁移:
Schema::create('surfaces', function (Blueprint $table) {
$table->increments('id');
$table->integer('apartment_id')->unsigned()->index();
$table->foreign('apartment_id')->references('id')->on('apartments')->onDelete('cascade');
$table->integer('floor_id')->unsigned()->index();
$table->foreign('floor_id')->references('id')->on('floors')->onDelete('cascade');
$table->decimal('surface',65, 3);
});
Schema::create('apartments', function (Blueprint $table) {
$table->increments('id');
$table->string('nom');
});
Schema::create('floors', function(Blueprint $table)
{
$table->increments('id');
$table->string('nom');
});
Schema::create('apartment_floor', function(Blueprint $table){
$table->increments('id');
$table->integer('apartment_id')->unsigned()->index();
$table->integer('floor_id')->unsigned()->index();
$table->foreign('apartment_id')->references('id')->on('apartments')->onDelete('cascade');
$table->foreign('floor_id')->references('id')->on('floors')->onDelete('cascade');
});
答案 0 :(得分:0)
我可以想到Laravel中的以下类型的关系,虽然虽然没有创建示例代码,但应该会有所帮助。
公寓型号:
public function floors() {
return $this->belongsTo('App\Models\Copro\Floor', 'floor_id', 'id');
} /* Many apartments belong to single floor. Single floor can have many apartments.*/
public function surfaces() {
return $this->hasMany('App\Models\Copro\Surface', 'surface_id', 'id');
} /* Single apartment has many surfaces */
地板模型:
public function apartments() {
return $this->hasMany('App\Models\Copro\Apartment', 'apartment_id', 'id');
} /* A floor has many apartments */
public function surfaces() {
return $this->hasMany('App\Models\Copro\Surface', 'surface_id', 'id');
} /* A floor has many surfaces */
表面模型:
public function floor() {
return $this->belongsTo('App\Models\Copro\Floor', 'floor_id', 'id');
} /* Surfaces belong to single floor */
public function apartment() {
return $this->hasMany('App\Models\Copro\Apartment', 'apartment_id', 'id');
} /* Surfaces belong to single apartment */
但是,如果您可以在同一问题上提供表架构或迁移架构,则会使答案更加清晰。此外,您还可以提及期望并要执行的SQL查询类型。谢谢。希望这会有所帮助。
编辑:
我现在还没有安装PHP,但是下面的模式对于我们至少给出更新更好的想法很有用。
Floor:
id
flr_number
Apartment:
id
apt_number
flr_id
Surface:
id
apt_id
surface
楼层将有许多通过地面的公寓。因此,我们可以在Floor模型中引入以下方法。
public function surfaces() {
return $this->hasManyThrough(
'App\Models\Copro\Surface',
'App\Models\Copro\Apartment',
'flr_id', /* Foreign key of Apartments table */
'apt_id', /* Foreign key of Surfaces table */
'id', /* Local key of Floors table */
'id' /* Local key of Apartments table */
);
}