fix: tree & guide

This commit is contained in:
2026-03-24 09:21:21 +08:00
parent b74ba1a3f8
commit 42a879e961
15 changed files with 2619 additions and 601 deletions

View File

@@ -3,42 +3,26 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use SolutionForest\FilamentTree\Concern\ModelTree;
class GuidePage extends Model
{
use ModelTree;
protected $fillable = [
'guide_id',
'page_number',
'title',
'html_url',
'sort_order',
'parent_id',
'options',
'branch_option',
];
protected $casts = [
'options' => 'array',
'parent_id' => 'int',
];
// filament-tree column name mapping
public function determineParentColumnName(): string
protected static function booted(): void
{
return 'parent_id';
}
public function determineOrderColumnName(): string
{
return 'sort_order';
}
public function determineTitleColumnName(): string
{
return 'title';
static::deleting(function (GuidePage $page) {
// CASCADE on from_page_id is handled by FK, but incoming edges need cleanup
GuidePageEdge::where('to_page_id', $page->id)->delete();
});
}
public function guide()
@@ -46,13 +30,32 @@ class GuidePage extends Model
return $this->belongsTo(Guide::class);
}
public function branchChildren()
public function outgoingEdges()
{
return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order');
return $this->hasMany(GuidePageEdge::class, 'from_page_id')->orderBy('sort');
}
public function parentPage()
public function incomingEdges()
{
return $this->belongsTo(self::class, 'parent_id');
return $this->hasMany(GuidePageEdge::class, 'to_page_id');
}
public function nextPages()
{
return $this->belongsToMany(self::class, 'guide_page_edges', 'from_page_id', 'to_page_id')
->withPivot('label', 'sort')
->orderByPivot('sort');
}
public function previousPages()
{
return $this->belongsToMany(self::class, 'guide_page_edges', 'to_page_id', 'from_page_id')
->withPivot('label', 'sort');
}
public function isEntry(): bool
{
return !$this->incomingEdges()->exists();
}
}