Laravel Eloquent, difference between attach() and syncWithoutDetaching()

I'm wondering if there is a difference between the two following Eloquent instructions that update the data on the role_user pivot table of a many-to-many relationship between User and Role models.

// Attach the specified roles to the user
$user–>roles()–>attach([1, 2, 3]);

// Sync the specified roles without detaching the one previously assigned to the user
$user–>roles()–>syncWithoutDetaching([1, 2, 3]);

I think they do the same thing, am I correct?

Read more here:

Content Attribution

This content was originally published by Davide Casiraghi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: