FixLikes.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\{Like, Status};
  5. use DB;
  6. class FixLikes extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'fix:likes';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Fix Like counts';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $chunk = 100;
  37. $limit = Like::select('status_id')->groupBy('status_id')->get()->count();
  38. if($limit > 1000) {
  39. if($this->confirm('We have found more than 1000 records to update, this may take a few moments. Are you sure you want to continue?') == false) {
  40. $this->error('Cancelling command...');
  41. return;
  42. }
  43. }
  44. $bar = $this->output->createProgressBar($limit);
  45. $this->line(' ');
  46. $this->info(' Starting like count fix ...');
  47. $this->line(' ');
  48. $bar->start();
  49. Like::selectRaw('count(id) as count, status_id')
  50. ->groupBy(['status_id','id'])
  51. ->chunk($chunk, function($likes) use($bar) {
  52. foreach($likes as $like) {
  53. $s = Status::find($like['status_id']);
  54. if($s && $s->likes_count == 0) {
  55. $s->likes_count = $like['count'];
  56. $s->save();
  57. }
  58. $bar->advance();
  59. }
  60. });
  61. $bar->finish();
  62. $this->line(' ');
  63. $this->line(' ');
  64. }
  65. }