我有一个代码可以迭代数据库中的动态字段。
类似这样的东西:
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
表:
它在保存数据时有效,但是在我尝试编辑特定产品时不显示数据。
如何显示中间表中的数据(属性与产品之间的关系为BelongsToMany)?在这种情况下我不知道。