public static function deleteWhereIDsNotIn(array $ids, $key = 'id')
{
// If the number of entries is lower than, or equals to 65535, just go ahead.
if (count($ids) <= 65535) {
return static::whereNotIn($key, $ids)->delete();
}
// Otherwise, we get the actual IDs that should be deleted…
$allIDs = static::select($key)->get()->pluck($key)->all();
$whereInIDs = array_diff($allIDs, $ids);
// …and see if we can delete them instead.
if (count($whereInIDs) < 65535) {
return static::whereIn($key, $whereInIDs)->delete();
}
// If that's not possible (i.e. this array has more than 65535 elements, too)
// then we'll delete chunk by chunk.
return static::deleteByChunk($ids, $key);
}