fix: tree & guide
This commit is contained in:
@@ -33,14 +33,22 @@ class Guide extends Model
|
||||
|
||||
public function pages()
|
||||
{
|
||||
return $this->hasMany(GuidePage::class)->orderBy('sort_order');
|
||||
return $this->hasMany(GuidePage::class);
|
||||
}
|
||||
|
||||
public function trunkPages()
|
||||
public function edges()
|
||||
{
|
||||
return $this->hasMany(GuidePage::class)
|
||||
->where('parent_id', -1)
|
||||
->orderBy('sort_order');
|
||||
return $this->hasMany(GuidePageEdge::class);
|
||||
}
|
||||
|
||||
public function entryPage()
|
||||
{
|
||||
return $this->hasOne(GuidePage::class)
|
||||
->whereNotIn('guide_pages.id', function ($q) {
|
||||
$q->select('to_page_id')
|
||||
->from('guide_page_edges')
|
||||
->whereColumn('guide_page_edges.guide_id', 'guide_pages.guide_id');
|
||||
});
|
||||
}
|
||||
|
||||
public function creator()
|
||||
@@ -76,7 +84,7 @@ class Guide extends Model
|
||||
|
||||
return $query->where(function (Builder $q) use ($stationIds) {
|
||||
$q->whereDoesntHave('stations')
|
||||
->orWhereHas('stations', fn ($sq) => $sq->whereIn('stations.id', $stationIds));
|
||||
->orWhereHas('stations', fn($sq) => $sq->whereIn('stations.id', $stationIds));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
31
app/Models/GuidePageEdge.php
Normal file
31
app/Models/GuidePageEdge.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class GuidePageEdge extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'guide_id',
|
||||
'from_page_id',
|
||||
'to_page_id',
|
||||
'label',
|
||||
'sort',
|
||||
];
|
||||
|
||||
public function guide()
|
||||
{
|
||||
return $this->belongsTo(Guide::class);
|
||||
}
|
||||
|
||||
public function fromPage()
|
||||
{
|
||||
return $this->belongsTo(GuidePage::class, 'from_page_id');
|
||||
}
|
||||
|
||||
public function toPage()
|
||||
{
|
||||
return $this->belongsTo(GuidePage::class, 'to_page_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user