1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/8/13
- * Time: 10:59
- */
- namespace App\Console\Commands;
- use App\Models\PostComment;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Redis;
- class UpdateReplyCount extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post:update_reply_count';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新评论回复数量';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(PostComment $postComment)
- {
- parent::__construct();
- $this->postComment = $postComment;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始更新评论回复数量");
- $bar = $this->output->createProgressBar($this->postComment->where('parent_id', 0)->count());
- $this->postComment->where('parent_id', 0)->chunk(100, function($comments) use ($bar){
- foreach($comments as $comment){
- $replyCount = $this->postComment->where('parent_id', $comment->id)->count();
- if($replyCount){
- $comment->reply_count = $replyCount;
- $comment->save();
- }
- $bar->advance();
- }
- usleep(100000);
- });
- $bar->finish();
- $this->line("\n更新评论回复数量结束");
- }
- }
|