2020-03-29 01:03:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Contracts\Model;
|
2020-06-11 20:27:38 +08:00
|
|
|
use App\Exceptions\UnknownPageType;
|
|
|
|
use App\Models\Enums\PageType;
|
2020-03-29 01:03:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property int id
|
|
|
|
* @property string name
|
|
|
|
* @property string slug
|
|
|
|
* @property string icon
|
|
|
|
* @property int type
|
|
|
|
* @property bool public
|
|
|
|
* @property bool enabled
|
2020-06-11 20:27:38 +08:00
|
|
|
* @property bool new_window
|
2020-03-29 01:03:52 +08:00
|
|
|
* @property string body
|
2020-06-11 20:27:38 +08:00
|
|
|
* @property string link
|
2020-03-29 01:03:52 +08:00
|
|
|
*/
|
|
|
|
class Page extends Model
|
|
|
|
{
|
|
|
|
public $table = 'pages';
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'slug',
|
|
|
|
'type',
|
|
|
|
'icon',
|
|
|
|
'public',
|
|
|
|
'body',
|
2020-06-11 20:27:38 +08:00
|
|
|
'link',
|
2020-03-29 01:03:52 +08:00
|
|
|
'enabled',
|
2020-06-11 20:27:38 +08:00
|
|
|
'new_window',
|
2020-03-29 01:03:52 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
2020-06-11 20:27:38 +08:00
|
|
|
'type' => 'integer',
|
|
|
|
'public' => 'boolean',
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
'new_window' => 'boolean',
|
2020-03-29 01:03:52 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
public static $rules = [
|
|
|
|
'name' => 'required|unique:pages,name',
|
2020-06-11 20:27:38 +08:00
|
|
|
'body' => 'nullable',
|
|
|
|
'type' => 'required',
|
2020-03-29 01:03:52 +08:00
|
|
|
];
|
2020-06-11 20:27:38 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the full URL to this page; determines if it's internal or external
|
|
|
|
*
|
|
|
|
* @throws \App\Exceptions\UnknownPageType
|
|
|
|
*/
|
|
|
|
public function getUrlAttribute(): string
|
|
|
|
{
|
|
|
|
if ($this->type === PageType::PAGE) {
|
|
|
|
return url(route('frontend.pages.show', ['slug' => $this->slug]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->type === PageType::LINK) {
|
|
|
|
return $this->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new UnknownPageType($this);
|
|
|
|
}
|
2020-03-29 01:03:52 +08:00
|
|
|
}
|