gpt4 book ai didi

mysql - Laravel和Mysql三表关系

转载 作者:行者123 更新时间:2023-11-30 21:42:05 25 4
gpt4 key购买 nike

我需要向 MySql 添加三个表,但我不知道如何组织它们。

我的表是:

  • 公寓
  • 楼层
  • 表面

还有我的亲戚:

  • 公寓可以有多层
  • 公寓可以有多个表面
  • 楼层可以有很多公寓
  • 地板可以有很多表面
  • 表面可以有一层(在公寓的楼层内)
  • 表面可以有一个公寓

表面是在公寓之后进入的,所以我不能使用表面地板作为我的公寓地板。

目前我有带数据透视表的公寓 table 和落地 table (多对多)。

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

最佳答案

我可以想到 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 */
);
}

关于mysql - Laravel和Mysql三表关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com