Laravel:3个桌子与中央枢轴相连。如何将此翻译成Laravel Eloquent ORM?

时间:2014-01-30 05:42:09

标签: laravel laravel-4 eloquent

您好,感谢您阅读本文,

Model

学生有几门课程和一个分配到该课程的个人项目。 可以使用多个单独的项目,但课程只有一个项目。

如果我想知道为课程1分配给学生10的个别项目..我可以这样问。

Select Individual_project.* FROM Individual_project
INNER JOIN Students_has_Course ON Individual_project.id = 
    Students_has_Course.Individual_project_id
INNER Course ON Students_has_Course.Course_id = Course.id
INNER JOIN Students ON Students_has_Course.Students_id=Students.id
wHere Students.id=10 AND Course.id=1

我正在尝试使用Eloquent将这些关系翻译成Laravel模型,但我认为我错过了一些东西。

很容易建立关系,找到与学生相关的课程..

class Students extends Eloquent {

    public function courses()
    {
        return $this->belongsToMany('Course','Students_has_Course','Course_id');
    }

}

但我不知道如何进行复杂的加入以获得一系列学生的课程以及每个学生的相关个人项目。

再次感谢您阅读本文。我希望这是可以理解的。英语不是我的第一语言,现在已经很晚了。如有必要,我会提供更多细节。

1 个答案:

答案 0 :(得分:0)

您的模型有问题。您不应将课程和项目放在同一个数据透视表中。当一个学生在一门课程中有3个项目并且您查询您的数据透视表时会发生什么?你会得到类似的东西:

john doe =>
    courses =>
        course 1 =>
            project 1 data
        course 1 =>
            project 2 data
        course 1 =>
            project 3 data

最好将不同的关系分解为单独的数据透视表。这是一项更多的工作,但它提供了更多的灵活性。请注意,我更改了表/列名称以匹配Laravel约定。

class Course extends Eloquent {

    protected $table = 'courses';

    public function projects() {
        return $this->belongsToMany('Project', 'course_project', 'course_id', 'project_id');
    }

    public function students() {
        return $this->belongsToMany('Student', 'course_student', 'course_id', 'student_id');
    }

}

class Student extends Eloquent {

    protected $table = 'students';

    public function courses() {
        return $this->belongsToMany('Course', 'course_student', 'course_id', 'student_id');
    }

    public function projects() {
        return $this->belongsToMany('Project', 'project_student', 'project_id', 'student_id');
    }

}

class Project extends Eloquent {

    protected $table = 'projects';

    public function courses() {
        return $this->belongsToMany('Course', 'course_student', 'course_id', 'student_id');
    }

    public function students() {
        return $this->belongsToMany('Student', 'course_student', 'course_id', 'student_id');
    }   

}

$studentsCoursesProjects = Student::with('courses', 'courses.projects')->get()->toArray();

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John Doe
            [courses] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Intro to Laravel
                            [pivot] => Array
                                (
                                    [course_id] => 1
                                    [student_id] => 1
                                )

                            [projects] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1
                                            [desc] => Routing
                                            [pivot] => Array
                                                (
                                                    [course_id] => 1
                                                    [project_id] => 1
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 2
                                            [desc] => Eloquent
                                            [pivot] => Array
                                                (
                                                    [course_id] => 1
                                                    [project_id] => 2
                                                )

                                        )

                                )

                        )

                )

        )

)

$studentsProjectsCourses = Student::with('projects', 'projects.courses')->get()->toArray();

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John Doe
            [projects] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [desc] => Routing
                            [pivot] => Array
                                (
                                    [project_id] => 1
                                    [student_id] => 1
                                )

                            [courses] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1
                                            [name] => Intro to Laravel
                                            [pivot] => Array
                                                (
                                                    [course_id] => 1
                                                    [student_id] => 1
                                                )

                                        )

                                )

                        )

                )

        )

)

这样做可以让你用Eloquent做各种很酷的事情。

$studentsCoursesProjectsFilter = Student::with(array('courses' => function($query) { }, 'courses.projects' => function ($query ) {
    $query->where('projects.id', 1);
}))->get()->toArray();

编辑:

如果无法更改数据库模型,那么您可以执行以下操作:

class Student extends Eloquent {

    protected $table = 'students';

    public function courses() {
        return $this->belongsToMany('Course', 'course_student', 'course_id', 'student_id');
    }

    public function projects() {
        return $this->belongsToMany('Project', 'course_student', 'project_id', 'course_id');
    }

}
$students = Student::with('courses', 'courses.projects')->get()->toArray();