神不在的星期二
我知道您在类别模型与其本身之间存在自引用关系,例如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 之间的关系表示为 Select 字段,而是表示为 BelongsTo,例如: BelongsTo::make('Parent Category', 'parent', Category::class)->searchable()->nullable(),但是您可以使用“选择”字段来预加载类别数组,这样您就可以仅在OnForms() 中过滤出当前类别。你可以这样做: 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(), ];}