Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fares.shawa's avatar

attendance group by date

Hello, this might seem like a beginner question, but I have been trying at this for days with no luck.

I have an attendance model linked using eloquent with User model as shown here

public function users()
   {
       return $this->BelongsToMany(User::class);
   }
public function attendances()
   {
       return $this->BelongsToMany(Attendance::class);
   }

The controller method index

public function index()
    {
        $attendances =Attendance::all();
        dd($attendances);
        $date = Carbon::now();
        $date = $date->toDateString();
        $present = Attendance::where('status' , 'Present')->where('date' , $date)->get();
        $absent = Attendance::where('status' , 'Absent')->where('date' , $date)->get();
        return view('attendance.index',compact('attendances','present','absent','date'));
    }

I want in the view to display the total number of users present for the day the attendance is taken, currently the only way I managed to do is getting it to work through comparing today's date with the absent - present, which is wrong. so to further display here is the view

@foreach($attendances as $attendance)
                                            <tr role="row" class="odd">
                                                <td class="sorting_1">{{count($present)}} Present</td>
                                                <td class="sorting_1">{{count($absent)}} Absent</td>
                                                <td>{{$attendance->date}}</td>
                                                <td>
                                                    <a class="btn btn-square btn-info" href="#" role="button">View</a>
                                                    <a class="btn btn-square btn-warning" href="#" role="button">Edit</a>
                                                </td>
                                            </tr>
@endforeach 

Hope I didn't over confuse my question..

0 likes
1 reply

Please or to participate in this conversation.