diff --git a/app/Models/Page.php b/app/Models/Page.php new file mode 100644 index 0000000..626d45a --- /dev/null +++ b/app/Models/Page.php @@ -0,0 +1,54 @@ + 'array', + 'is_published' => 'boolean', + 'main' => 'boolean', + 'linked' => 'boolean', + ]; + + protected static function boot() { + parent::boot(); + static::creating(function ($page) { + $page->uuid = (string) Str::uuid(); + }); + } + + public function activeRevision(){ + return $this->hasOne(PageRevision::class)->where('active', true); + } + + public function author() { + return $this->belongsTo(User::class, 'user_id'); + } + + public function revisions() { + return $this->hasMany(PageRevision::class); + } +} diff --git a/app/Models/PageRevision.php b/app/Models/PageRevision.php new file mode 100644 index 0000000..a532f3b --- /dev/null +++ b/app/Models/PageRevision.php @@ -0,0 +1,55 @@ + 'array', + ]; + + protected static function boot() { + parent::boot(); + static::creating(function ($revision) { + $revision->uuid = (string) Str::uuid(); + + if (!$revision->version) { + $latestVersion = self::where('page_id', $revision->page_id)->max('version') ?? 0; + $revision->version = $latestVersion + 1; + } + }); + } + + public function getRouteKeyName(): string { + return 'uuid'; + } + + public function page() { + return $this->belongsTo(Page::class); + } + + public function editor() { + return $this->belongsTo(User::class, 'user_id'); + } +}