表:项目
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_departmentsproject_id = 48和projects_departmentsproject_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/

10-16 07:46