gpt4 book ai didi

php - 我想连接两个表,在基本模型中,它们的 id 在 Laravel 中是未知的,我不知道如何获取准确的结果

转载 作者:行者123 更新时间:2023-11-29 15:45:29 25 4
gpt4 key购买 nike

我正在尝试从faculties表中获取数据。但我选择了注册。基本上我的想法是显示使用教师姓名和部分注册的类(class)

我尝试了所有连接,但没有任何效果。我很困惑如何在 Laravel Eloquent 中进行 subjoin。

$results = Enrollment::select(
'students.rollnumber as student_rollnumber',
'courses.name as course_name',
'courses.course_code',
'sections.name as section_name',
'faculties.fname as faculty_fname',
'faculties.lname as faculty_lname'
)->join(
'courses',
'courses.id',
'enrollments.course_id'
)->join(
'students',
'students.id',
'enrollments.student_id'
)->join(
'sections',
'sections.id',
'enrollments.section_id'
)->join(
'faculties',
'faculties.section_id',
'enrollments.section_id'
)->join(
'faculty_course_details',
'faculties.id',
'faculty_course_details.faculty_id'
)->where('enrollments.student_id',9)
->get();

错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'faculties.fname' in 'field list' (SQL: select students.rollnumber as student_rollnumber, courses.name as course_name, courses.course_code, sections.name as section_name, faculties.fname as faculty_fname, faculties.lname as faculty_lname from enrollments inner join courses on courses.id = enrollments.course_id inner join students on students.id = enrollments.student_id inner join sections on sections.id = enrollments.section_id where enrollments.student_id = 9)

最佳答案

您在加入院系和院系_类(class)_详细信息时犯了错误。您正在使用列section_id加入学院,而学院表中没有section_id列

首先加入(faculty_course_details.section_id、enrollments.section_id)上的faculty_course_details,然后加入(faculties.id、faculty_course_details.faculty_id)上的学院,如下所示

$results = App\Enrollment::select(
'students.rollnumber as student_rollnumber',
'courses.name as course_name',
'courses.course_code',
'sections.name as section_name',
'faculties.fname as faculty_fname',
'faculties.lname as faculty_lname'
)->join(
'courses',
'courses.id',
'enrollments.course_id'
)->join(
'students',
'students.id',
'enrollments.student_id'
)->join(
'sections',
'sections.id',
'enrollments.section_id'
)->join(
'faculty_course_details',
'faculty_course_details.section_id',
'enrollments.section_id'
)->join(
'faculties',
'faculties.id',
'faculty_course_details.faculty_id'
)
->where('enrollments.student_id',9)
->get();

您将得到如下输出:

[
{
"student_rollnumber": "dummy_878wegy6",
"course_name": "dummy_course",
"course_code": "dummy_123",
"section_name": "dummy_name",
"faculty_fname": "John",
"faculty_lname": "Doe"
}
]

关于php - 我想连接两个表,在基本模型中,它们的 id 在 Laravel 中是未知的,我不知道如何获取准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com