Laravel Eloquent 建立国家与城市关系的好方法 table
Laravel Eloquent A good way to make relationship between country state and city table
我有 3 个表,架构如下
countries (rows: 250)
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| code | varchar(255) | NO | UNI | | |country code
| phone_code | varchar(255) | NO | | | |
| region | varchar(255) | NO | | | |
| subregion | varchar(255) | NO | | | |
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+------------+---------------------+------+-----+---------+----------------+
states (rows: 4866)
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| country_code| varchar(255) | NO | MUL | | | this is country code
| state_code | varchar(255) | YES | | | |
| lat | varchar(255) | YES | | | |
| lon | varchar(255) | YES | | | |
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+-------------+---------------------+------+-----+---------+----------------+
cities (rows: 146068)
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| lat | varchar(255) | YES | | | |
| lng | varchar(255) | YES | | | |
| population | varchar(255) | YES | | | |
| state_code | varchar(255) | NO | | | |state code not unique
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+------------+---------------------+------+-----+---------+----------------+
我正在使用 quickadminpanel 生成这些 CRUD 但主要问题是我从 csv 文件中导入了这些
git link for csv and csvimport trait like https://pastebin.com/G9z8Rjf1
有什么方法可以在这三个表之间建立关系
country:code and state:country_code
关系和 state:state_code and city:state_code
关系,因为我无法手动添加州(行:4866)和城市(行:146068)
那么我如何使用模型或任何更好的方式或任何更好的建立关系的特征来建立关系?
只需将每个table的Primary Keys分别改成code,state_code,city_code
注意:将 state_code 州 table 和 city_code 城市 table[=13 中的倍数更改为唯一=]
并在您的模型中改变关系,例如
/* return $this->hasMany(Model::class, 'foreign_key', 'local_key');
*
return $this->hasMany(State::class, 'country_code', 'code');
和
/// return $this->belongsTo(Model::class, 'foreign_key', 'owner_key');///
return $this->belongsTo(Country::class, 'code', 'country_code');
然后您就可以正常访问所有数据了..
我有 3 个表,架构如下
countries (rows: 250)
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| code | varchar(255) | NO | UNI | | |country code
| phone_code | varchar(255) | NO | | | |
| region | varchar(255) | NO | | | |
| subregion | varchar(255) | NO | | | |
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+------------+---------------------+------+-----+---------+----------------+
states (rows: 4866)
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| country_code| varchar(255) | NO | MUL | | | this is country code
| state_code | varchar(255) | YES | | | |
| lat | varchar(255) | YES | | | |
| lon | varchar(255) | YES | | | |
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+-------------+---------------------+------+-----+---------+----------------+
cities (rows: 146068)
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | | auto_increment |
| name | varchar(255) | NO | | | |
| lat | varchar(255) | YES | | | |
| lng | varchar(255) | YES | | | |
| population | varchar(255) | YES | | | |
| state_code | varchar(255) | NO | | | |state code not unique
| created_at | timestamp | YES | | | |
| updated_at | timestamp | YES | | | |
| deleted_at | timestamp | YES | | | |
+------------+---------------------+------+-----+---------+----------------+
我正在使用 quickadminpanel 生成这些 CRUD 但主要问题是我从 csv 文件中导入了这些 git link for csv and csvimport trait like https://pastebin.com/G9z8Rjf1
有什么方法可以在这三个表之间建立关系
country:code and state:country_code
关系和 state:state_code and city:state_code
关系,因为我无法手动添加州(行:4866)和城市(行:146068)
那么我如何使用模型或任何更好的方式或任何更好的建立关系的特征来建立关系?
只需将每个table的Primary Keys分别改成code,state_code,city_code
注意:将 state_code 州 table 和 city_code 城市 table[=13 中的倍数更改为唯一=]
并在您的模型中改变关系,例如
/* return $this->hasMany(Model::class, 'foreign_key', 'local_key');
*
return $this->hasMany(State::class, 'country_code', 'code');
和
/// return $this->belongsTo(Model::class, 'foreign_key', 'owner_key');///
return $this->belongsTo(Country::class, 'code', 'country_code');
然后您就可以正常访问所有数据了..