如何从blade视图中与fk连接的3表中获取值

vs3odd8k  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(201)

表:项目在此处输入图像描述
表:项目\此处为部门图像描述表:此处为部门图像描述
这是样板工程

public function projects_departments() {
    return $this->hasMany(Project_department::class); 
}

这是模型项目部

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]:找不到列:“where子句”中的1054未知列“projects\u departments.project\u id”(sql:select*from) projects_departments 哪里 projects_departments . project_id =48和 projects_departments . project_id (不为空)

kqqjbcuj

kqqjbcuj1#

$project = DB::table('projects')
        ->select(*)
        ->join('Department', 'projects_departments.department_id', '=', 'Department.id')
        ->join('projects_departments', 'projects_departments.project_id', '=', 'projects.id')

        ->get();

让我们试试这个

相关问题