PublishScheduledPosts.php 801 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Post;
  4. use Illuminate\Console\Command;
  5. class PublishScheduledPosts extends Command
  6. {
  7. protected $signature = 'posts:publish-scheduled';
  8. protected $description = 'Публикация запланированных постов';
  9. public function handle()
  10. {
  11. $posts = Post::readyForPublishing()->get();
  12. if ($posts->isEmpty()) {
  13. $this->info('Нет постов для публикации');
  14. return 0;
  15. }
  16. foreach ($posts as $post) {
  17. $post->publish();
  18. $this->info("Опубликован пост: {$post->title}");
  19. }
  20. $this->info("Всего опубликовано постов: {$posts->count()}");
  21. return 0;
  22. }
  23. }