- $episode->channels()
- ->where('ext_id', 'LIKE', 'sg:%')
- ->whereNotIn('ext_id', $ext_ids)
- ->detach();
+ $channels = $episode->channels()
+ ->where('ext_id', 'LIKE', 'sg:%')
+ ->whereNotIn('ext_id', $ext_ids)
+ ->get();
+ if (!$channels->isEmpty()) {
+ $episode->channels()->detach($channels->pluck('id'));
+ }