Ask Your Question
0

How can I use Laravel's Eloquent to retrieve the most recent attendance record of each student?

asked 2021-07-12 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-30 23:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-07-12 11:00:00 +0000

Seen: 7 times

Last updated: Nov 30 '21