. */ namespace App\Console\Commands; use Illuminate\Console\Command; use App\Models\User; class RebuildArtists extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'rebuild:artists'; /** * The console command description. * * @var string */ protected $description = 'Recounts every user\'s tracks, ensuring that the artist directory isn\'t missing anyone.'; /** * Create a new command instance. * */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $numberOfUsers = User::count(); $bar = $this->output->createProgressBar($numberOfUsers); foreach (User::with(['tracks' => function ($query) { $query->published()->listed(); }])->get() as $user) { $bar->advance(); $user->track_count = $user->tracks->count(); $user->save(); } $bar->finish(); $this->line(''); } }