It seems like the issue you're encountering is related to the Eloquent relationship caching. When you update the pivot table directly using the DB facade, Eloquent doesn't automatically refresh the loaded relationships on the model. To ensure that the relationship data is fresh, you need to reload the relationship after performing the update.
Here's how you can modify your markAttendance method to ensure that the relationship is reloaded:
public function markAttendance(OrientationClassAttendanceRequest $request, OrientationClass $oclass)
{
$validated = $request->validated();
$attendees = $validated['attendanceList'];
$relation = $oclass->users();
DB::table($relation->getTable())
->where($relation->getForeignPivotKeyName(), $oclass->getKey())
->whereIn($relation->getRelatedPivotKeyName(), $attendees)
->update([
'attended' => true
]);
// Reload the 'users' relationship to get fresh data
$oclass->load('users');
return Redirect::route('classes.edit', ['oclass' => $oclass])->with('success', 'Attendance marked');
}
In the edit method, you should now have the updated relationship data:
public function edit(OrientationClass $oclass)
{
$attendees = [];
if (Request::input('search')) {
$search = Request::input('search');
$attendees = User::query()->where('last_name', 'like', "%{$search}%")
->orWhere('first_name', 'like', "%{$search}%")
->orWhere('member_number', 'like', "%{$search}%")
->get();
}
// No need to call $oclass->refresh() if you've already reloaded the relationship
return Inertia::render('Classes/Edit', [
'oclass' => [
'id' => $oclass->id,
'attendees' => $oclass->users, // This should now contain the updated pivot data
'date' => $oclass->date,
'formatted_date' => Carbon::parse($oclass->date)->toFormattedDayDateString()
],
'attendees' => $attendees,
'filters' => Request::only(['search'])
]);
}
By calling $oclass->load('users'); after the update, you're telling Eloquent to refresh the users relationship on the $oclass model, which should include the updated pivot data. This way, when you access $oclass->users in your edit method, it will reflect the changes made in the database.