HashtagCachedCountUpdate.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Hashtag;
  5. use App\StatusHashtag;
  6. use DB;
  7. class HashtagCachedCountUpdate extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'app:hashtag-cached-count-update {--limit=100}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Update cached counter of hashtags';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle()
  25. {
  26. $limit = $this->option('limit');
  27. $tags = Hashtag::whereNull('cached_count')->limit($limit)->get();
  28. $count = count($tags);
  29. if(!$count) {
  30. return;
  31. }
  32. $bar = $this->output->createProgressBar($count);
  33. $bar->start();
  34. foreach($tags as $tag) {
  35. $count = DB::table('status_hashtags')->whereHashtagId($tag->id)->count();
  36. if(!$count) {
  37. $tag->cached_count = 0;
  38. $tag->saveQuietly();
  39. $bar->advance();
  40. continue;
  41. }
  42. $tag->cached_count = $count;
  43. $tag->saveQuietly();
  44. $bar->advance();
  45. }
  46. $bar->finish();
  47. $this->line(' ');
  48. return;
  49. }
  50. }