Update an existing pivot record

If you want to update an existing pivot record on the table, use updateExistingPivot instead of syncWithPivotValues.

1// Migrations
2Schema::create('role_user', function ($table) {
3 $table->unsignedId('user_id');
4 $table->unsignedId('role_id');
5 $table->timestamp('assigned_at');
6})
7 
8// first param for the record id
9// second param for the pivot records
10$user->roles()->updateExistingPivot(
11 $id, ['assigned_at' => now()],
12);

Tip given by @sky_0xs

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord