Between the Activity, Attendance, and Attendee; there are a few pivots to help keep track of the data that is found within the relation between the main models.
attendance_attendee
The attendance_attendee
pivot stores if the attendee was present or not for that attendance.
activity_attendee
The activity_attendee
stores the status we normally associate with attendances – Present, Late, Absent, etc…
Import Process
When I’m importing all of the attendance records, I basically just populate the attendance_attendee
table. I have model events that fire which trigger a listener that updates the activity_attendee
status.
class AttendanceImport implements ToCollection
{
public function collection(Collection $rows)
{
$rows->each(function ($row) {
...
AttendanceAttendee::updateOrCreate([
"attendee_id" => $attendee->id,
"attendance_id" => $attendance->id,
"activity_id" => $attendance->activity->id,
], [
"is_present" => true
]);
....
});
...
}
}
AttendanceAttendee
record is updated or created so is_present
is true for the attendee when the attendance was taken.
class AttendanceAttendee extends Pivot
{
protected $fillable = [
"attendee_id",
"attendance_id",
"activity_id",
"is_present"
];
protected $dispatchesEvents = [
"saved" => AttendanceAttendeeSaved::class
];
...
}
AttendanceAttendee
fires the AttendanceAttendeeSaved
event
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
...
AttendanceAttendeeSaved::class => [
UpdateActivityAttendanceOnAttendanceAttendee::class,
],
ActivityAttendeeSaved::class => [
UpdateAttendeeEoOnActivityAttendeeSaved::class,
SendAttendeeAbsentNotificationListener::class
],
];
...
}
The EventServiceProvider
watches for the AttendanceAttendeeSaved
event and fires the UpdateActivityAttendanceOnAttendanceAttendee
listener. A listener that updates the ActivityAttendance
record status.
Thus whenever I import an attendance record, the attendee’s activity status is automatically updated!
Interested in reading more about my Attendance Tracker project? Click below for more articles!