Dipen Chand
Dipen Chand

Reputation: 145

Getting result from array of Ids in Laravel

I've a following attendance table:

id | grade_id | subject_id |    date    |        students
 1 |     2    |      6     | 2020-05-05 | [3,6,8,11,17,21,20,19] 

I want to fetch all rows with name of all students from array of Ids. What I've tried is:

$result[] = Attendance::where('date', '=', $day)
                ->with('grades', 'subjects')
                ->join('students', function($join) {
                      $join->on('students.id', '=', 'attendances.id')
                           ->where('students.id', '=', 'attendances.students');
                      })->get();

But couldn't get result. Please help me!

Upvotes: 0

Views: 141

Answers (1)

Rana Nadeem
Rana Nadeem

Reputation: 1225

It is not a proper table structure.Will be better if you make another table like

id | attendance_id | student_id
1  | 1             | 3
1  | 1             | 6

And make its relation in model with attendance and student table.This way table will be normalized and will be easy to handle relationship.

Upvotes: 1

Related Questions