changes
This commit is contained in:
@@ -14,7 +14,24 @@ class Page extends Model
|
||||
|
||||
protected $guarded = false;
|
||||
|
||||
public function section() : BelongsTo
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// Auto-generate path when page is created
|
||||
static::creating(function (Page $page) {
|
||||
$page->generatePath();
|
||||
});
|
||||
|
||||
// Only regenerate path if slug or parent section changed
|
||||
static::updating(function (Page $page) {
|
||||
if ($page->isDirty(['slug', 'sub_section_id'])) {
|
||||
$page->generatePath();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function section(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SubSection::class, 'sub_section_id');
|
||||
}
|
||||
@@ -28,4 +45,45 @@ class Page extends Model
|
||||
'content' => 'array',
|
||||
'settings' => 'array',
|
||||
];
|
||||
|
||||
/**
|
||||
* Generate path for the page based on slug and parent section
|
||||
*/
|
||||
protected function generatePath(): void
|
||||
{
|
||||
// Don't regenerate path for registered pages (system routes)
|
||||
if ($this->exists && $this->is_registered) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only generate path if slug is present
|
||||
if (empty($this->slug)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get sub_section_id from model attributes
|
||||
$subSectionId = $this->sub_section_id;
|
||||
|
||||
if ($subSectionId === null) {
|
||||
$this->path = $this->slug;
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch subSection with mainSection relationship
|
||||
$subSection = SubSection::with('mainSection')->find($subSectionId);
|
||||
|
||||
if ($subSection === null) {
|
||||
$this->path = $this->slug;
|
||||
return;
|
||||
}
|
||||
|
||||
$mainSection = $subSection->mainSection;
|
||||
|
||||
if ($mainSection === null) {
|
||||
$this->path = $subSection->slug . '/' . $this->slug;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->path = $mainSection->slug . '/' . $subSection->slug . '/' . $this->slug;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user