gpt4 book ai didi

php - 如何在 Laravel 上构建合并和构建组查询?

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

现在我使用这个查询来显示下面的结果查询:

 $data['faculty'] = Faculty::select('faculties.id', 'faculties.faculty', 'faculties.slug', 'faculties.status','s.semester','fs.faculty_id')
->join('faculty_semester as fs', 'faculties.id', '=', 'fs.faculty_id')
->join('semesters as s', 's.id', '=', 'fs.semester_id')
->orderBy('faculties.faculty', 'asc')
->get();

显示结果

1     BBA      FIRST SEM
2 BBA SECOND SEM
3 BBA FOURTH SEM
4 BBA THIRD SEM
5 BBS SECOND YEAR
6 BBS FIRST YEAR

但我想在组中显示。如何在 Laravel 上构建组查询以显示结果下方

1     BBA      FIRST SEM, 
SECOND SEM
THIRD SEM
FOURTH SEM
2 BBS FIRST YEAR
SECOND YEAR

最佳答案

我建议使用预先加载,在模型中声明关系

https://laravel.com/docs/5.5/eloquent-relationships#eager-loading , 这样你就可以避免使用连接..

你试过吗?

$data['faculty'] = Faculty::select('faculties.id', 'faculties.faculty', 'faculties.slug', 'faculties.status','s.semester','fs.faculty_id')
->join('faculty_semester as fs', 'faculties.id', '=', 'fs.faculty_id')
->join('semesters as s', 's.id', '=', 'fs.semester_id')
->groupBy('faculties.faculty')
->get();

关于php - 如何在 Laravel 上构建合并和构建组查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47204453/

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