belongsToMany(User::class)->withPivot('status', 'assigned_by')->withTimestamps(); } } class User extends Model { public function roles() { return $this->belongsToMany(Role::class)->withPivot('status', 'assigned_by')->withTimestamps(); } } // Attaching a role with pivot data $user = User::find(1); $user->roles()->attach(2, ['status' => 'active', 'assigned_by' => 3]); // Syncing roles (detaches missing, attaches new, updates existing) $user->roles()->sync([ 1 => ['status' => 'active', 'assigned_by' => 3], 3 => ['status' => 'pending', 'assigned_by' => 1] ]); // Updating pivot data for an existing relationship $user->roles()->updateExistingPivot(1, ['status' => 'inactive']); // Accessing pivot data foreach ($user->roles as $role) { echo $role->name . " (Status: " . $role->pivot->status . ", Assigned by: " . $role->pivot->assigned_by . ") "; }