Search code examples
sqllaraveleloquent-relationship

How to access old value on intermediate table while updateExistingPivot() in laravel 8


Let's say Here . I have an extra column on intermediate table 'noOfUpdates' then how can i access its previous value.

I tried this but it didn't work.

$user = User::find(1);

$user->roles()->updateExistingPivot($roleId, [
    'noOfUpdates' => noOfUpdates + 1,
]);

In raw SQL i can to do that. n_enroll column

sorry for my english :(

PLEASE HELP


Solution

  • you can use newPivotQuery along with increment, newPivotQuery creates a new query builder for the pivot table.

    $user->roles()->newPivotQuery()->where('user_id',$user->id)
    ->where('role_id',$roleId)->increment('noOfUpdates');