代码之家  ›  专栏  ›  技术社区  ›  derrickrozay

使用雄辩式查询多个表

  •  0
  • derrickrozay  · 技术社区  · 8 年前

    我用的是Laravel 5.6。我正在尝试从 grading_info 但是我还想返回学生的名字和其他信息 student_info 表。我只想返回 评分\信息 与当前登录的教师相关的表。目前它为所有教师返回信息。我知道我可以加一个 where 但是我想学习雄辩,想知道是否有什么方法可以做到这一点?

    教师和学生可以在 评分\信息 桌子和任何老师都可以给任何学生评分。

    我想退像这样的东西

    {
        gradeID,
        gradeDate,
        gradeInfo
        .....
        student: {
            studentName,
            studentPhoneNumber,
            studentEmail
            ......
        }
    }
    

    用户表(仅存储教师,不存储学生)

    • 身份证件

    教师信息

    • 教学ID(链接到用户表中的ID)

    学生信息

    • ID(自动递增。与用户表无关)

    评分\信息

    • student id(链接到学生信息中的ID)
    • 教学ID(链接到来自用户的ID)

    用户模型

    public function grades(){
        return $this->hasMany(GradingInfo::class, 'studentID');
    }
    

    分级信息模型

    public function teacher(){
        return $this->belongsTo(User::class, 'id', 'teacherID');
    }
    
    public function student() {
        return $this->belongsTo(StudentInfo::class, 'studentID', 'id');
    }
    

    studentinfo模型

    public function grades() {
        return $this->hasMany(SessionInfo::class, 'studentID', 'id');
    }
    

    教学信息模型

    // Nothing in here. 
    

    示教器控制器

    public function getGrades(Request $request)
    {
        $user       = Auth::user(); // This is the teacher
        $grades     = $user->with('sessions.student')->orderBy('created_at', 'DESC')->get();
        return response()->json(['sessions' => $sessions], 200);
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   rkj    8 年前

    你有 Many to Many 之间的关系 user(teacher) student(student_info) tables

    用户模型

    public function gradeStudents(){
        return $this->belongsToMany(StudentInfo::class, 'grading_info', 'teacherID', 'studentID');
    }
    
    public function info(){  //get teacher info 
       return $this->hasOne(TeacherInfo::class, 'teacherID');
    }
    

    studentinfo模型

    public function gradeTeachers(){
            return $this->belongsToMany(User::class, 'grading_info', 'studentID', 'teacherID');
    }
    

    现在获取数据(教学控制器)

    public function getGrades(Request $request)
    {
        $user     = Auth::user(); // This is the teacher
        $students = $user->gradeStudents; // it will return all graded students by logged in teacher 
        return response()->json(['students' => $students], 200);
    }
    

    在这里 grading_info 是多对多关系的透视表

    有关详细信息,请检查此 https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

    从透视表获取额外信息

    如果要在透视表中添加额外信息( 评分\信息 )然后添加列( info )在这个表中,然后需要像这样更改关系

    public function gradeStudents(){
            return $this->belongsToMany(StudentInfo::class, 'grading_info', 'teacherID', 'studentID')
                        ->withPivot('info')
                        ->as('grade')
                        ->withTimestamps();              
    }
    

    现在,如果你提取数据

     $user = Auth::user(); // This is the teacher
     $students = $user->gradeStudents; 
    
     foreach($students as $student){
        print_r($student);
        print_r($student->grade->info);
        print_r($student->grade->created_at);
     }
    
        2
  •  0
  •   مهدی عابدی برنامه نویس و مشاور    8 年前

    您应该在您的模型中定义与HasOne、HasMany的关系。 像这样的事情:

    class Review extends Eloquent {
        public function relatedGallery()
        {
            $this->hasOne('Gallery', 'foreign_id', 'local_id');
        }
    }
    
    class Gallery extends Eloquent {
        public function relatedReviews()
        {
            $this->hasMany('Review', 'foreign_id', 'local_id');
        }
    }
    
    $gallery = Gallery::with('relatedReviews')->find($id);
    Will bring the object Gallery with
    
    $gallery->id
    gallery->name
    ...
    
    $gallery->relatedReviews // array containing the related Review Objects