表:项目
enter image description here
表格:projects_departments enter image description here
表:部门enter image description here
这是模型项目
public function projects_departments() {
return $this->hasMany(Project_department::class);
}
这是模型Project_department
public function projects()
{
return $this->belongsTo(Project::class);
}
public function departments()
{
return $this->belongsTo(Department::class);
}
这是模特部
public function projects()
{
return $this->hasMany(Project::class);
}
public function projects_departments() {
return $this->hasMany(Project_department::class);
}
这是我的刀片
project->departments->department_name
这是我的错误
SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ projects_departments.project_id”(SQL:从
projects_departments
中选择*,其中projects_departments
。project_id
= 48和projects_departments
。project_id
是不为空) 最佳答案
$project = DB::table('projects')
->select(*)
->join('Department', 'projects_departments.department_id', '=', 'Department.id')
->join('projects_departments', 'projects_departments.project_id', '=', 'projects.id')
->get();
让我们试试这个
关于mysql - 我如何从3张 table 中获得值(value)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53185383/