post = $post; $this->topic = $topic; } /** * Execute the console command. * * @return mixed */ public function handle() { $this->line("开始更新话题使用数"); $topic = []; $bar = $this->output->createProgressBar($this->post->withTrashed()->count()); $this->post->withTrashed()->chunk(100, function($posts) use ($bar, &$topic){ foreach($posts as $post) { $topicIds = explode(',', $post->topic_ids); foreach($topicIds as $id){ if(isset($topic[$id])){ $topic[$id] += 1; }else{ $topic[$id] = 1; } } $bar->advance(); } usleep(100000); }); foreach($topic as $id => $useCount){ $this->topic->where('id', $id)->update(['use_count' => $useCount]); } $bar->finish(); $this->line("\n更新话题使用数结束"); } }