$editorRoles * @property string $name * @property int|null $next_id * @property Page|null $next * @property int $page_id * @property int|null $previous_id * @property Page|null $previous * @property Collection $roles * @property string $slug * @property Collection $viewerRoles * * @method static PageFactory factory(...$parameters) */ #[Hidden([Page::ATTRIBUTE_BODY])] #[ScopedBy(ReadablePagesScope::class)] #[Table(Page::TABLE, Page::ATTRIBUTE_ID)] class Page extends BaseModel implements Auditable, SoftDeletable { use HasAudits; use HasFactory; use SoftDeletes; final public const string TABLE = 'pages'; final public const string ATTRIBUTE_BODY = 'body'; final public const string ATTRIBUTE_ID = 'page_id'; final public const string ATTRIBUTE_NAME = 'name'; final public const string ATTRIBUTE_NEXT = 'next_id'; final public const string ATTRIBUTE_PREVIOUS = 'previous_id'; final public const string ATTRIBUTE_SLUG = 'slug'; final public const string RELATION_EDITOR_ROLES = 'editorRoles'; final public const string RELATION_NEXT = 'next'; final public const string RELATION_PREVIOUS = 'previous'; final public const string RELATION_ROLES = 'roles'; final public const string RELATION_VIEWER_ROLES = 'viewerRoles'; /** * The event map for the model. * * Allows for object-based events for native Eloquent events. * * @var array */ protected $dispatchesEvents = [ 'created' => PageCreated::class, 'deleted' => PageDeleted::class, 'restored' => PageRestored::class, 'updated' => PageUpdated::class, ]; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ Page::ATTRIBUTE_BODY, Page::ATTRIBUTE_NAME, Page::ATTRIBUTE_NEXT, Page::ATTRIBUTE_PREVIOUS, Page::ATTRIBUTE_SLUG, ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ Page::ATTRIBUTE_BODY => 'string', Page::ATTRIBUTE_NAME => 'string', Page::ATTRIBUTE_NEXT => 'int', Page::ATTRIBUTE_PREVIOUS => 'int', Page::ATTRIBUTE_SLUG => 'string', ]; } /** * Get the route key for the model. * * @noinspection PhpMissingParentCallCommonInspection */ public function getRouteKeyName(): string { return Page::ATTRIBUTE_SLUG; } public function getName(): string { return $this->name; } public function getSubtitle(): string { return $this->slug; } public function isPublic(): bool { return $this->viewerRoles()->doesntExist(); } /** * @return BelongsTo */ public function next(): BelongsTo { return $this->belongsTo(Page::class, Page::ATTRIBUTE_NEXT); } /** * @return BelongsTo */ public function previous(): BelongsTo { return $this->belongsTo(Page::class, Page::ATTRIBUTE_PREVIOUS); } /** * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, PageRole::TABLE, PageRole::ATTRIBUTE_PAGE, PageRole::ATTRIBUTE_ROLE) ->using(PageRole::class) ->withPivot([PageRole::ATTRIBUTE_ID, PageRole::ATTRIBUTE_TYPE]) ->withTimestamps(); } /** * @return BelongsToMany */ public function viewerRoles(): BelongsToMany { return $this->roles()->wherePivot(PageRole::ATTRIBUTE_TYPE, PageRoleType::VIEWER->value); } /** * @return BelongsToMany */ public function editorRoles(): BelongsToMany { return $this->roles()->wherePivot(PageRole::ATTRIBUTE_TYPE, PageRoleType::EDITOR->value); } }