PublishScheduledPosts.php 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Post;
  5. class PublishScheduledPosts extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'blog:publish-scheduled';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Публикация отложенных статей';
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle()
  23. {
  24. //
  25. $count = Post::where('is_published', false)
  26. ->whereNotNull('scheduled_at')
  27. ->where('scheduled_at', '<=', now())
  28. ->update([
  29. 'is_published' => true,
  30. 'published_at' => now()
  31. ]);
  32. $this->info("Опубликовано статей: " . $count);
  33. }
  34. }