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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-07-12 11:00:00 +0000
Seen: 7 times
Last updated: Nov 30 '21