PublishScheduledArticles.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Article;
  5. class PublishScheduledArticles extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'app:publish-scheduled-articles';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Command description';
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle()
  23. {
  24. $pending_up = Article::pending()->get();
  25. $pending_down = Article::published()->whereNotNull("unpublish_at")->get();
  26. foreach ($pending_up as $article) {
  27. if ($article->publish_at->isPast()) {
  28. $this->info("Publishing [$article->title] at " . now());
  29. $article->publish();
  30. }
  31. }
  32. foreach ($pending_down as $article) {
  33. if ($article->unpublish_at->isPast()) {
  34. $this->info("Taking down [$article->title] at " . now());
  35. $article->unpublish();
  36. }
  37. }
  38. }
  39. }