| Conditions | 4 |
| Paths | 2 |
| Total Lines | 35 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 32 | public function handle(): int |
||
| 33 | { |
||
| 34 | $this->info('Selecting all duplicates...'); |
||
| 35 | |||
| 36 | $subQuery = Account::query() |
||
| 37 | ->select('username', app('db')->raw('COUNT(id) AS total')) |
||
| 38 | ->groupBy('username') |
||
| 39 | ->orderBy('total', 'DESC') |
||
| 40 | ->toSql(); |
||
| 41 | |||
| 42 | /** @var \Illuminate\Support\Collection $results */ |
||
| 43 | $results = app('db')->table(app('db')->raw(" ({$subQuery}) AS subq")) |
||
| 44 | ->where('total', '>', 1) |
||
| 45 | ->get(); |
||
| 46 | |||
| 47 | if ($results->count() > 0) { |
||
| 48 | foreach ($results as $result) { |
||
| 49 | $this->info("Removing {$result->username}..."); |
||
| 50 | $deletedRows = Account::query() |
||
| 51 | ->where('username', $result->username) |
||
| 52 | ->orderBy('updated_at', 'ASC') |
||
| 53 | ->take(1) |
||
| 54 | ->delete(); |
||
| 55 | if ($deletedRows === 1) { |
||
| 56 | $this->info('Deleted'); |
||
| 57 | } else { |
||
| 58 | $this->error('Error!'); |
||
| 59 | } |
||
| 60 | $this->info('--------------------------------------------'); |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | $this->info('No duplicates found'); |
||
| 64 | } |
||
| 65 | |||
| 66 | return 0; |
||
| 67 | } |
||
| 69 |