Laravel Nova 4 属于取决于

问题描述 投票:0回答:1

我有两个表,类别和产品,我将类别 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);
}),

laravel-nova
1个回答
0
投票

应该是类似这样的东西

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);
           });
        }
    })
© www.soinside.com 2019 - 2024. All rights reserved.