【问题标题】:Using eloquent to query multiple tables使用 eloquent 查询多张表
【发布时间】:2018-06-19 14:46:28
【问题描述】:

我正在使用 Laravel 5.6。我正在尝试从grading_info 表中查询信息,但我也想从student_info 表中返回学生姓名和其他信息。我只想返回grading_info 表中与当前登录的教师相关的记录。目前它为所有教师返回信息。我知道我可以添加一个where 子句,但我正在努力学习雄辩,想知道是否有任何方法可以做到这一点?

教师和学生可以在grading_info 表中拥有许多条目,并且任何教师都可以对任何学生进行评分。

我想退回这样的东西

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

users 表(只存储教师,不存储学生)

  • 身份证

教师信息

  • teacherID(链接到用户表中的 id)

学生信息

  • id(自动递增。与 users 表无关)

分级信息

  • studentID(链接到来自 student_info 的 id)
  • teacherID(链接到用户的 id)

用户模型

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

GradingInfo 模型

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

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

学生信息模型

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);
}

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    user(teacher)student(student_info) tables 之间存在 Many to Many 关系

    用户模型

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

    学生信息模型

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

    现在获取数据(TeacherController)

    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

    从数据透视表中获取额外信息

    如果您想在数据透视表 (@​​987654329@) 中添加额外信息,请在此表中添加列 (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);
     }
    

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2019-07-21
      • 2021-08-27
      • 2017-09-11
      • 2020-12-29
      • 2013-05-10
      相关资源
      最近更新 更多