Skip to content

Instantly share code, notes, and snippets.

@asilbalaban
Created February 23, 2022 08:52
Show Gist options
  • Select an option

  • Save asilbalaban/81e5f583d97b893c722df4e61767398b to your computer and use it in GitHub Desktop.

Select an option

Save asilbalaban/81e5f583d97b893c722df4e61767398b to your computer and use it in GitHub Desktop.
remove duplicate unique column rows in laravel
<?php
Route::get('test', function () {
$raw = \DB::raw('count(*) as total');
$requestCounts = \App\Models\Cards::query()->select('card_slug', $raw)->havingRaw('COUNT(*) > 1')->groupBy('card_slug')->limit(500)->pluck('total', 'card_slug');
if (ob_get_level() == 0) ob_start();
foreach ($requestCounts as $key => $val) {
echo $key . ' - ' . $val . '<br>';
$ids = \App\Models\Cards::where('card_slug', $key)->pluck('id');
unset($ids[0]);
\App\Models\Cards::whereIn('id', $ids)->delete();
ob_flush();
flush();
echo '<script>var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;</script>';
}
ob_end_flush();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment