| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Models\Post;
- class PublishPostsCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'posts:publish';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Публикует отложенные посты';
-
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $posts = Post::where('is_published', false)
- ->whereNotNull('published_at')
- ->where('published_at', '<=', now())
- ->get();
- foreach ($posts as $post) {
- $post->update(['is_published' => true]);
- $this->info("Пост {$post->id} опубликован.");
- }
- }
- }
|