What is the difference between attach()
and sync()
in Laravel 4's Eloquent ORM? I've tried to look around but couldn't find anything!
3 Answers
attach():
- Insert related models when working with many-to-many relations
- No array parameter is expected
Example:
$user = User::find(1);
$user->roles()->attach(1);
sync():
Similar to the attach()
method, the sync()
method is used to attach related models. However, the main differences are:
sync()
accepts an array of IDs to place on the pivot table- Secondly, most important, the sync method will delete the data from the pivot table if the model does not exist in the array, and insert only the new items to the pivot table.
Example:
user_role
id user_id role_id 1 12 1 2 12 5 3 12 2
$user = User::find(12);
$user->roles()->sync(array(1, 2, 3));
The above operation will delete:
id user_id role_id 2 12 5
And insert role_id 3
to the table.
user_role table
id user_id role_id 1 12 1 3 12 2 4 12 3

- 1,694
- 3
- 31
- 72

- 11,999
- 9
- 49
- 63
-
20Thank you! So in theory, using `$user()->roles()->detach($oldIDs)` followed by `$user()->roles()->attach($newIDs)` is the same as `$user()->roles()->sync($newIDs)`, right? – Kousha May 31 '14 at 23:25
-
30Also there is one second argument for sync() that is by default true, if you pass false, then the non matching will not be deleted. Refer: http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Relations/MorphToMany.html#method_sync – Musa Haidari Dec 18 '14 at 06:24
-
get last insert id in sync? – Deenadhayalan Manoharan Mar 10 '15 at 10:06
-
1@Deena actually pivot tables don't need any ID - so you don't get any ID back :) but you can get the related ones by reusing the model object variable – Seiji Manoan May 26 '15 at 20:18
-
How about when pivot table does not only have 2 columns(user_id, role_id). I want add 1 column is value to pivot table, how can I add data to that column – Henry Bui Sep 14 '17 at 08:18
-
I'm not sure when it was introduced, but there now exists a `syncWithoutDetaching()` method - is the only difference between `attach()` and `syncWithoutDetaching()` that the latter can accept an array of IDs? – CGriffin Nov 02 '17 at 16:04
-
@CGriffin I find it interesting that `attach/detach` methods have also `$touch` parameter (not really documented well, but may update timestamps?) - both `sync` and `attach` touch based on `protected $touches = [];`, but in case of `attach` you can manually turn it off ...For basic operations there should be no difference though.... – jave.web May 21 '19 at 12:09
-
1Small Note: The attach() method accept arrays of IDs or single value as input. The same goes for the detach() method. – Maik Lowrey Jan 25 '21 at 09:12
To make it even simpler:
The attach
function only adds records to the Pivot table.
The sync
function replaces the current records with the new records. This is very useful for updating a model.
Example:
Assuming you have a created Post that has many Tags attached on it where the Tags ID's are [1,2,3].
And the user has the ability to update the Post and its Tags.
The user will send you the new Tags ID's [3,4,5].
If you use the sync
function, the new Tags of the Post will be [3,4,5] only.
If you use the attach
function, the new Tags of the Post will be [1,2,3,4,5].

- 438
- 2
- 11

- 30,478
- 7
- 87
- 83
-
my problem is similar to this . i can use your answer to solve it thanks. https://stackoverflow.com/a/36573783/308578 – saber tabatabaee yazdi May 19 '20 at 21:59
Posted answers don't mention syncWithoutDetaching
, so here is my simple take.
If you want to:
- add the model to the relation, allowing for duplicates, use
attach()
- add the models to the relation, ignoring duplicates, use
syncWithoutDetaching()
- set the relation to the exact models, use
sync()
Example:
Calling:
$user->articles()->attach(1);
$user->articles()->attach(2);
$user->articles()->attach(1);
echo $user->articles;
Will return articles: 1, 2, 1
Calling:
$user->articles()->syncWithoutDetaching([1]);
$user->articles()->syncWithoutDetaching([2]);
$user->articles()->syncWithoutDetaching([1]);
echo $user->articles;
Will return articles: 1, 2
Calling:
$user->articles()->sync([1]);
$user->articles()->sync([2, 3]);
$user->articles()->sync([3, 4]);
echo $user->articles;
Will return articles: 3, 4

- 1,394
- 12
- 19