You can use the groupBy()
and orderBy()
functions of Eloquent to retrieve the most recent attendance record of each student. Here's an example:
$attendances = DB::table('attendances')
->select(DB::raw('MAX(date) as date, student_id, status'))
->groupBy('student_id')
->orderBy('date', 'desc')
->get();
Here, we first select the maximum date for each student using the MAX()
function and group the results by the student_id
. Then, we order the results by the date
in descending order to get the most recent attendance record for each student.
Note that this is just an example and the actual implementation may vary depending on your database schema and requirements.
Asked: 2021-07-12 11:00:00 +0000
Seen: 14 times
Last updated: Nov 30 '21