我有以下两个表: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 | |帐户和项目之间存在一对多的关系。表中的外键projects被命名account_code而不是account_id.重要的项目标识符列的问题不在project_id项目id表中。这是我的帐户模型: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我尝试检索accountproject_id312所属的详细信息。$project = \App\Project::where('project_id', 312)->get();dd($project->account->name);我怎样才能在不写查询的情况下解决这个问题?
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
这里的问题是 get 正在检索你一个集合而不是一个实例。您可以使用
$project = \App\Project::where('project_id', 312)->first();
相反,这应该可以解决问题。
希望能帮助到你。这里公开了其他一些解决方案:Property [title] does not exist on this collection instance
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报
0/150
提交
取消