I've a method looks like this:
public function saveContacts(Request $request)
{
if($request->contacts) {
$contacts = collect($request->contacts)->pluck('id');
$this->contacts()->sync($contacts->toArray());
}
}
$request->contacts
You can do this:
if ($request->contacts) {
$contacts = collect($request->contacts)->pluck('id')->toArray();
if (empty($contacts)) {
$this->contacts()->detach();
} else {
$this->contacts()->sync($contacts);
}
}