Asked thg 3 24, 2021 12:14 CH 142 0 2
  • 142 0 2
0

[Laravel] Không thể select dữ liệu từ nhiều bảng dữ liệu

Share
  • 142 0 2

Hiện tại e có 4 model là Semester( Học kì ), Teacher ( Giáo Viên ), Class ( Lớp học ), Subject ( Môn học). Nếu như dùng query builder để join các bảng lại thì có nhiều dữ liệu trùng cột. Vậy cho e hỏi cách nào tốt nhất để có thể lấy toàn bộ dữ liệu 4 bảng ạ. Tiện thể cho e hỏi cách xây dựng database của e có ổn k ạ. Em cảm ơn nhiều.

class SemesterModel extends Model
{
    public function class()
    {
        return $this->belongsToMany(ClassModel::class, 'semester_class', 'semester_id', 'class_id');
    }
    public function teacher()
    {
        return $this->belongsToMany(TeacherModel::class, 'semester_teacher', 'semester_id', 'teacher_id');
    }
    public function subject()
    {
        return $this->belongsToMany(SubjectModel::class, 'semester_subject', 'semester_id', 'subject_id');
    }
}
class TeacherModel extends Model
{
    public function class()
    {
        return $this->belongsToMany(ClassModel::class, 'teacher_class', 'teacher_id', 'class_id');
    }
    public function semester()
    {
        return $this->belongsToMany(SemesterModel::class, 'semester_teacher', 'teacher_id', 'semester_id');
    }
    public function subject()
    {
        return $this->belongsToMany(SubjectModel::class, 'semester_subject', 'teacher_id', 'subject_id');
    }
}
class ClassModel extends Model
{
    public function teacher()
    {
        return $this->belongsToMany(TeacherModel::class, 'teacher_class', 'teacher_id', 'class_id');
    }
    public function semester()
    {
        return $this->belongsToMany(SemesterModel::class, 'semester_class', 'class_id', 'semester_id');
    }
    public function subject()
    {
        return $this->belongsToMany(SubjectModel::class, 'class_subject', 'class_id', 'subject_id');
    }
}
class SubjectModel extends Model
{
    public function teacher()
    {
        return $this->belongsToMany(TeacherModel::class, 'teacher_subject', 'subject_id', 'teacher_id');
    }
    public function semester()
    {
        return $this->belongsToMany(SemesterModel::class, 'semester_subject', 'subject_id', 'semester_id');
    }
    public function class()
    {
        return $this->belongsToMany(ClassModel::class, 'class_subject', 'subject_id', 'class_id');
    }
}

2 ANSWERS


Answered thg 3 25, 2021 1:03 SA
Accepted
0

@ungthanhlong251198 Bạn thử dùng eager loading hoặc lazy loading nhé.
https://laravel.com/docs/8.x/eloquent-relationships#eager-loading

Share
Avatar Thành Long Ứng @ungthanhlong251198
thg 3 25, 2021 1:28 SA

e cảm ơn ạ

0
| Reply
Share
Answered thg 3 25, 2021 10:12 SA
0

bạn có thể join rồi select kiểu semester. * , teacher . * ,.v.v.v là các cột sẽ riêng biệt mà

Share
Avatar Thành Long Ứng @ungthanhlong251198
thg 3 25, 2021 12:46 CH

e.PNG output nó ra như v thì làm sao để gom 1 lớp với nhiều môn học ạ. Giống cách gom của with trong Eloquent ORM ạ

0
| Reply
Share
Avatar iamfresher @benkyou
thg 3 26, 2021 1:45 SA

@nanapham trường hợp các table có tên column trùng nhau thì nó sẽ bị ghi đè thì phải bạn ạ. mình nhớ là vậy.

0
| Reply
Share
Avatar newbie @nanapham
thg 3 26, 2021 4:29 SA

@benkyou nếu bạn chỉ select * thì nó sẽ bị ghi đè, còn bạn select kiểu semester. * , teacher . * thì nó sẽ là semester.id, teacher.id .v.v.v

0
| Reply
Share
Avatar newbie @nanapham
thg 3 26, 2021 4:30 SA
Avatar newbie @nanapham
thg 3 26, 2021 4:47 SA

@ungthanhlong251198 bạn dùng concat thử xem nhé

SELECT ID, GROUP_CONCAT(idSubject), GROUP_CONCAT(nameSubject), 
FROM table_name
GROUP BY ID
0
| Reply
Share
Avatar iamfresher @benkyou
thg 3 29, 2021 3:54 SA

@nanapham Bạn thử chưa ? Mình thử nó vẫn ghi đè đó 😄
Trừ khi bạn dùng AS để đặt cho nó 1 cái name khác thì mới ko bị nhé.
Hình bên dưới:
Cả 2 bảng Orders và Customers đều có trường CustomerID
Khi SELECT Orders., Customers. thì nó ko ra 2 trường mà chỉ còn 1 trường thui.

Capture.PNG

0
| Reply
Share
Avatar newbie @nanapham
thg 4 1, 2021 1:48 SA

@benkyou my bad

0
| Reply
Share
Avatar Hưng Duy @hoangduyhunglc
thg 4 7, 2021 4:27 SA
Avatar Hưng Duy @hoangduyhunglc
thg 4 7, 2021 4:30 SA

Hi bạn! Mình không biết bạn đã có cách giải quyết vấn đề của bạn chưa nhưng đây là cách mình thưởng sử dụng. Bên dưới là 1 ví dụ của mình dùng trong phân quyền, bạn tham khảo nhé Trong model Screen Shot 2021-04-07 at 11.28.07.png

Trong service Screen Shot 2021-04-07 at 11.28.37.png

Khi trỏ with vào cái relationship trong model là đã lấy join được nhiều bảng mình mong muốn rồi

0
| Reply
Share
Viblo
Let's register a Viblo Account to get more interesting posts.