PublishPostsCommand.php 882 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Post;
  5. class PublishPostsCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'posts:publish';
  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. $posts = Post::where('is_published', false)
  25. ->whereNotNull('published_at')
  26. ->where('published_at', '<=', now())
  27. ->get();
  28. foreach ($posts as $post) {
  29. $post->update(['is_published' => true]);
  30. $this->info("Пост {$post->id} опубликован.");
  31. }
  32. }
  33. }