Просмотр файла app/Console/Commands/DeleteFiles.php

Размер файла: 1Kb
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\Models\File;
  6. use Illuminate\Console\Command;
  7.  
  8. class DeleteFiles extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'delete:files';
  16.  
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Delete unattached files';
  23.  
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33.  
  34. /**
  35. * Удаляет не прикрепленные файлы
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $files = File::query()
  42. ->where('relate_id', 0)
  43. ->where('created_at', '<', strtotime('-1 day', SITETIME))
  44. ->get();
  45.  
  46. foreach ($files as $file) {
  47. $file->delete();
  48. }
  49.  
  50. $this->info('Files successfully deleted.');
  51.  
  52. return 0;
  53. }
  54. }