1 回答

TA貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊
我知道您在類別模型與其本身之間存在自引用關(guān)系,例如
class Category extends Model
{
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
}
在 Nova 中,通常,您不會將 Child 與其 Parent 之間的關(guān)系表示為 Select 字段,而是表示為 BelongsTo,例如:
BelongsTo::make('Parent Category', 'parent', Category::class)->searchable()->nullable(),
但是您可以使用“選擇”字段來預(yù)加載類別數(shù)組,這樣您就可以僅在OnForms() 中過濾出當(dāng)前類別。
你可以這樣做:
public function fields(Request $request)
{
$fields = [
// [ All your fields ]
// We'll use a Select but onlyOnForms to show all categories but current category when in Forms
Select::make('Parent', 'parent_id')->options(Category::where('id', '!=', request()->resourceId)->pluck('name', 'id'))->onlyOnForms(),
// Use a BelongsTo to show the parent category when in Details page
BelongsTo::make('Parent', 'parent', Category::class)->searchable()->nullable()->showOnDetail()->hideWhenCreating()->hideWhenUpdating(),
];
}
- 1 回答
- 0 關(guān)注
- 134 瀏覽
添加回答
舉報