当前位置:  开发笔记 > 编程语言 > 正文

如何在急切加载中获取关联表的行:Laravel 5.1

如何解决《如何在急切加载中获取关联表的行:Laravel5.1》经验,为你挑选了0个好方法。

我有三个数据库表.

CREATE TABLE `tblproject` (
  `ProjectID` int(11) NOT NULL,
  `ProjectStatusID` varchar(30) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE `tblprojectSkills` (
  `ProjectSkillID` int(11) NOT NULL,
  `ProjectID` int NOT NULL,
  `SkillID` int NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE `tblSkills` (
  `SkillID` int(11) NOT NULL,
  `Skill` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

在上表中.SkillID在相关tblSkillstblprojectSkills. ProjectID在相关ProjectprojectSkills

我的项目模型如下.

class Project_Model extends Model
{
    protected $table = "tblproject";
    protected $primaryKey = "ProjectID";
    public $timestamps = false;

    public function ProjectSkills() {
        return $this->hasMany('\App\Models\ProjectSkill_Model', 'ProjectID');
    }        
}

class ProjectSkill_Model extends Model
{
    protected $table = "tblprojectskill";
    protected $primaryKey = "ProjectSkillID";
    public $timestamps = false;
}

class Skill_Model extends Model
{
    protected $table = "tblskill";
    protected $primaryKey = "SkillID";
    public $timestamps = false;
}

laravel 5.1中的数据库查询如下.

\App\Models\Project\Project_Model
::with('ProjectSkills')
->where('ProjectID', '=', $ProjectID)->first();

我可以获得技能ID,但是,我如何从技能表中获得技能名称?

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有