'boolean', 'pages_public' => 'boolean', 'pages_published_at' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'pages_custom_css' => 'string', 'pages_custom_js' => 'string' ]; protected $dates = [ 'deleted_at' ]; /** * Gets the user who created the page. */ public function creator() { return $this->belongsTo(\Modules\Auth\Models\User::class, 'pages_created_by'); } /** * Gets the user who last updated the page. */ public function updater() { return $this->belongsTo(\Modules\Auth\Models\User::class, 'pages_updated_by'); } /** * Scope for published pages. */ public function scopePublished($query) { return $query->where('pages_is_published', true) ->where('pages_published_at', '<=', \Carbon\Carbon::now()); } /** * Scope for unpublished pages. */ public function scopeDraft($query) { return $query->where('pages_is_published', false); } /** * Gets the page URL. */ public function getUrlAttribute() { return url('pages/' . $this->pages_slug); } /** * Gets the content excerpt. */ public function getExcerptAttribute($length = 150) { $content = strip_tags($this->pages_content); return strlen($content) > $length ? substr($content, 0, $length) . '...' : $content; } /** * Publishes the page. */ public function publish() { $this->pages_is_published = true; $this->pages_published_at = \Carbon\Carbon::now(); return $this->save(); } /** * Sets the page as draft. */ public function unpublish() { $this->pages_is_published = false; return $this->save(); } /** * Checks if the page is published. */ public function isPublished() { return $this->pages_is_published && $this->pages_published_at <= \Carbon\Carbon::now(); } /** * Gets the page layout. */ public function getLayoutPath() { if ($this->pages_layout) { $layout = $this->pages_layout; if (substr($layout, -5) !== '.twig') { $layout .= '.twig'; } return $layout; } return 'layouts/base.twig'; } /** * Gets the page CSS classes. */ public function getCssClasses() { return $this->pages_css_classes ?: ''; } /** * Gets the page custom CSS. */ public function getCustomCss() { $css = ''; if ($this->pages_custom_css) { $css .= '' . "\n"; } return $css; } /** * Gets the page custom JavaScript. */ public function getCustomJs() { $js = ''; if ($this->pages_custom_js) { $js .= '' . "\n"; } return $js; } }