'datetime', 'scheduled_at' => 'datetime', ]; protected $dispatchesEvents = [ 'created' => PostPublished::class, ]; // Автоматическое создание slug protected static function boot() { parent::boot(); static::creating(function ($post) { if (empty($post->slug)) { $baseSlug = \Illuminate\Support\Str::slug($post->title); $slug = $baseSlug; $counter = 1; while (static::where('slug', $slug)->exists()) { $slug = $baseSlug . '-' . $counter; $counter++; } $post->slug = $slug; } }); } // Связь с комментариями public function comments() { return $this->hasMany(Comment::class); } // Только опубликованные посты public function scopePublished($query) { return $query->where('status', 'published') ->whereNotNull('published_at') ->where('published_at', '<=', now()); } // Посты, готовые к автопубликации public function scopeReadyForPublishing($query) { return $query->where('status', 'scheduled') ->whereNotNull('scheduled_at') ->where('scheduled_at', '<=', now()); } // Публикация поста public function publish() { $this->update([ 'status' => 'published', 'published_at' => now(), ]); event(new PostPublished($this)); } // Снятие с публикации public function unpublish() { $this->update([ 'status' => 'draft', ]); } // Планирование публикации public function schedule($dateTime) { $this->update([ 'status' => 'scheduled', 'scheduled_at' => $dateTime, ]); event(new PostScheduled($this)); } }