【发布时间】:2020-08-21 05:02:24
【问题描述】:
我有一个关注attendance 表:
id | grade_id | subject_id | date | students
1 | 2 | 6 | 2020-05-05 | [3,6,8,11,17,21,20,19]
我想从 ID 数组中获取所有学生姓名的所有行。 我试过的是:
$result[] = Attendance::where('date', '=', $day)
->with('grades', 'subjects')
->join('students', function($join) {
$join->on('students.id', '=', 'attendances.id')
->where('students.id', '=', 'attendances.students');
})->get();
但无法获得结果。请帮帮我!
【问题讨论】:
-
你应该已经被Many To Many relationship使用了。
-
在
students列上进行标准化会更容易 - 您可以使用数据透视表将学生 ID 与相关的出勤 ID 匹配,而不是将它们作为数组保存在单个列上。请检查这个链接,它解释得很好stackoverflow.com/a/17371788/2188922 -
我同意@Ersoy
-
谢谢大家!我使用@Ersoy 的方式实现了结果。
标签: php arrays laravel join where-in