laravel attach without duplicate code example
Example 1: laravel attach once
$user->roles()->syncWithoutDetaching([1, 2, 3]);
Example 2: laravel belongstomany prevent duplicates attach
$cart->items()->syncWithoutDetaching([$item->id]);
$user->roles()->syncWithoutDetaching([1, 2, 3]);
$cart->items()->syncWithoutDetaching([$item->id]);