Return table 字段中 Select 的所有行 laravel Nova

Return all table's rows in a Select field laravel Nova

我想 return 我的 select 我的 'categories' table 的所有元素,除了当前行。 我在网上没找到这方面的资料,所以我来找你。

我当前 Select 的项目:

Select::make('Parent Category')
                ->options([

            ])
                ->displayUsingLabels(),

这是我的类别之首 table:

我了解到您在类别模型与自身之间存在自引用关系,例如

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(),

但是您可以使用 Select 字段来预加载类别数组,这样您就可以过滤掉当前类别 onlyOnForms()。

你可以这样做:

 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(),
       
        ];


}