在Laravel Nova字段中显示关系数据

时间:2019-05-24 03:50:49

标签: laravel laravel-nova

我有一个代码可以迭代数据库中的动态字段。

类似这样的东西:

foreach (\App\AttributeGroup::getAllWithAttributes($request->route()->parameter('resourceId')) as $attributeGroup)
{
    $fields = collect($attributeGroup->attributes)->map(function ($attribute) {
        $class = 'Laravel\\Nova\\Fields\\' . ucfirst($attribute->type);

        $field = $class::make($attribute->name, $attribute->code)
            ->fillUsing(function ($request, $model, $requestAttribute) use ($attribute) {
                return function () use ($model, $request, $requestAttribute, $attribute) {
                    $model->attributes()->attach([
                        $attribute->id => ['value' => $request->post($requestAttribute)]
                    ]);
                };
            });

        // Only select dropdown menu has options
        if ($attribute->type == 'select') $field->options($attribute->getOptions());

        return $attribute->setValidation($field);
    })->filter()->all();

    $tabs[ $attributeGroup->name ] = $fields;
}

那是因为我使用fillUsing将每个字段中的数据保存到中间表中。

下面是attribute_product表:

enter image description here

它在保存数据时有效,但是在我尝试编辑特定产品时不显示数据。

如何显示中间表中的数据(属性与产品之间的关系为BelongsToMany)?在这种情况下我不知道。

enter image description here

0 个答案:

没有答案