我有以下两个表:
account:
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| id | varchar(25) | NO | PRI | | |
| name | varchar(191) | NO | UNI | NULL | |
| status | tinyint(3) unsigned | NO | | NULL | |
| active | tinyint(3) unsigned | NO | | NULL | |
project:
+-----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| client_code | varchar(191) | NO | | NULL | |
| tags | varchar(500) | YES | | NULL | |
| project_id | int(11) | NO | | NULL | |
| file_name | varchar(191) | NO | | NULL | |
| total_records | int(11) | NO | | NULL | |
客户和项目之间存在一对多关系。
表中的外键被命名为 而不是 。projectsaccount_codeaccount_id
问题是具有重要意义的项目标识符列,而不是在项目表中。project_idid
这是我的客户模型:
public function projects() {
return $this->hasMany('App\Project', 'account_code');
}
项目型号:
public function account() {
return $this->belongsTo('App\Account', 'account_code', 'project_id');
}
我在项目表中有一行作为:
id: 9
account_code: 0011T00002K95MLQAZ
tags: cpi|test|dmf
project_id: 312
file_name: conf_matrix_dev_1579064650.tsv
total_records: 19
我尝试检索project_id所属的详细信息。account312
$project = \App\Project::where('project_id', 312)->get();
dd($project->account->name);
如何在不编写查询的情况下解决此问题?
阿晨1998