php - 返回选择字段 laravel Nova 中的所有表行
问题描述
我想返回我的选择我的“类别”表的所有元素,除了当前行。我在互联网上没有找到任何关于此的信息,所以我来找你。
我当前选择的项目:
Select::make('Parent Category')
->options([
])
->displayUsingLabels(),
这是我的类别表的标题:
解决方案
我了解您在 Category 模型与其自身之间存在自引用关系,例如
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(),
];
}
推荐阅读
- python - 如何让我的飞机/敌人班级上下飞行
- r - 尝试移动刻度线并删除轴标题中的 X
- r - 为什么我会为“plot.margins”和“margins”获得不同的 AME?
- list - 方案-计算对数的程序(SICP ex 3.17)-我的解决方案正确吗?
- flutter - 我可以在一个 Flutter 插件项目中拥有多个 FlutterPlugin 吗?
- typescript - Redux Toolkit w TypeScript:如何解开 AsyncThunk 的类型
- c - 在 Linux libreadline 中解析带引号和双引号的行
- python-2.7 - 使用 pyspark 将每一行的一列作为单独的文件写入 hdfs
- javascript - 从 Chrome 扩展发出请求时,无法访问响应的 httpOnly(如“set-cookie”)标头
- r - 在 R 中使用 Pivot_longer 计算 NA