'boolean', 'published_at' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Boot the model. */ protected static function boot() { parent::boot(); static::creating(function ($article) { if (empty($article->slug)) { $article->slug = Str::slug($article->title); } }); static::updating(function ($article) { if ($article->isDirty('title') && empty($article->slug)) { $article->slug = Str::slug($article->title); } }); } /** * Scope to get only published articles. */ public function scopePublished($query) { return $query->where('is_published', true) ->where('published_at', '<=', now()); } /** * Get the route key for the model. */ public function getRouteKeyName(): string { return 'slug'; } /** * Get the excerpt attribute. */ public function getExcerptAttribute(): string { return Str::limit(strip_tags($this->content), 150); } /** * Get the formatted published date. */ public function getFormattedPublishedAtAttribute(): string { return $this->published_at->format('d M Y'); } }