我有两个表,类别和产品,我将类别 ID 设置为产品。在我的订单页面中,我想根据我在类别中选择的产品来选择产品。
这是我的代码,它在 laravel nova 4 中不起作用
BelongsTo::make('Category', 'category', Category::class)
->displayUsing(function ($category) {
return $category->name;
}),
BelongsTo::make('Product', 'product', Product::class)
->options(function ($query, $resource) {
return $query->where('category_uuid', $resource->uuid);
}),
应该是类似这样的东西
BelongsTo::make('Category', 'category', Category::class)
->displayUsing(function ($category) {
return $category->name;
}),
BelongsTo::make('Product', 'product', Product::class)
->dependsOn(['category'], function (BelongsTo $field, NovaRequest $request, $formData) {
if ($formData->category) {
$categoryId = $formData->category;
$field->relatableQuery(function ($query) use ($categoryId) {
return $query->where('category_id', $categoryId);
});
}
})