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

wonder95's avatar

Model relationship values not updated after db update

I have a Vue form for maintaining students and attendance for an orientation class (see here and here for background if interested), and the form has a checkbox for each row that I can select, and when I click on the appropriate button, it calls this controller method:

    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
            ]);

        return Redirect::route('classes.edit',['oclass' => $oclass])->with('success', 'Attendance marked');
    }

which then redirects to this method that displays the page

    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();
        }

        return Inertia::render('Classes/Edit', [
            'oclass' => [
                'id' => $oclass->id,
                'attendees' => $oclass->users,
                'date' => $oclass->date,
                'formatted_date' => Carbon::parse($oclass->date)->toFormattedDayDateString()
            ],
            'attendees' => $attendees,
            'filters' => Request::only(['search'])
        ]);
    }

The issue I'm having is that when the $oclass model is loaded/returned in edit(), the updated value from the markAttendance() method is not there. I have verified that the data is correctly updated in the db, but the $oclass->users() object is not updated. Even if I try to refresh like so:

        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();
        }

       $oclass->refresh();

        return Inertia::render('Classes/Edit', [
            'oclass' => [
                'id' => $oclass->id,
                'attendees' => $oclass->users,
                'date' => $oclass->date,
                'formatted_date' => Carbon::parse($oclass->date)->toFormattedDayDateString()
            ],
            'attendees' => $attendees,
            'filters' => Request::only(['search'])
        ]);

it isn't updated. How can I get $oclass->users updated so that props.attendees[i].pivot.attended has the updated value when the Inertia::render() is returned from the controller?

0 likes
2 replies
LaryAI's avatar
Level 58

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.

wonder95's avatar

Sorry, Larry, it doesn't work: I updated my code accordingly,

        DB::table($relation->getTable())
            ->where($relation->getForeignPivotKeyName(), $oclass->getKey())
            ->whereIn($relation->getRelatedPivotKeyName(), $attendees)
            ->update([
                'attended' => true
            ]);

        $oclass->load('users');

        return Redirect::route('classes.edit',['oclass' => $oclass])->with('success', 'Attendance marked');

but it's still not loaded. However, I did the same thing in the edit() method, and it worked fine.

        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();
        }

        $oclass->load('users');

        return Inertia::render('Classes/Edit', [
            'oclass' => [
                'id' => $oclass->id,
                'attendees' => $oclass->users,
                'date' => $oclass->date,
                'formatted_date' => Carbon::parse($oclass->date)->toFormattedDayDateString()
            ],
            'attendees' => $attendees,
            'filters' => Request::only(['search'])
        ]);

and it works fine.

Please or to participate in this conversation.