FixDuplicateProfiles.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\{
  5. Like,
  6. Media,
  7. Profile,
  8. Status,
  9. User
  10. };
  11. class FixDuplicateProfiles extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'fix:profile:duplicates';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Fix duplicate profiles';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $profiles = Profile::selectRaw('count(user_id) as count,user_id')->whereNotNull('user_id')->groupBy('user_id')->orderBy('user_id', 'desc')->get()->where('count', '>', 1);
  42. $count = $profiles->count();
  43. if($count == 0) {
  44. $this->info("No duplicate profiles found!");
  45. return;
  46. }
  47. $this->info("Found {$count} accounts with duplicate profiles...");
  48. $bar = $this->output->createProgressBar($count);
  49. $bar->start();
  50. foreach ($profiles as $profile) {
  51. $dup = Profile::whereUserId($profile->user_id)->get();
  52. if(
  53. $dup->first()->username === $dup->last()->username &&
  54. $dup->last()->statuses()->count() == 0 &&
  55. $dup->last()->followers()->count() == 0 &&
  56. $dup->last()->likes()->count() == 0 &&
  57. $dup->last()->media()->count() == 0
  58. ) {
  59. $dup->last()->avatar->forceDelete();
  60. $dup->last()->forceDelete();
  61. }
  62. $bar->advance();
  63. }
  64. $bar->finish();
  65. }
  66. }