PageBuilder models
This commit is contained in:
parent
2b5b1c43d7
commit
b8acb478b0
54
app/Models/Page.php
Normal file
54
app/Models/Page.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
use App\Models\PageRevision;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
class Page extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'uuid',
|
||||||
|
'title',
|
||||||
|
'slug',
|
||||||
|
'content',
|
||||||
|
'is_published',
|
||||||
|
'main',
|
||||||
|
'linked',
|
||||||
|
'linkorder',
|
||||||
|
'visibility',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'content' => '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);
|
||||||
|
}
|
||||||
|
}
|
55
app/Models/PageRevision.php
Normal file
55
app/Models/PageRevision.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
use App\Models\Page;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
class PageRevision extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'page_id',
|
||||||
|
'user_id',
|
||||||
|
'uuid',
|
||||||
|
'version',
|
||||||
|
'content',
|
||||||
|
'title',
|
||||||
|
'slug',
|
||||||
|
'label',
|
||||||
|
'active'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'content' => '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');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user