0

所以这个问题的出现是因为我正在研究的另一个问题。

因此,我涉足了后端表单的一些工作,并认为我知道如何解决这个问题。但是,我没有看到我认为的结果。我试图做一些研究,但没有看到有人在 github 页面或 stackoverflow 的问题中提出这个问题。除非我错过了。您如何在后端显示存储的额外数据透视表?

这是我的model.php关系:

public $belongsToMany = [
    'equipments' => [
        'Brandon\Pixelrpg\Models\Equipments',
        'table' => 'brandon_pixelrpg_equipment_inventory',
        'key' => 'inventory',
        'otherKey' => 'equipment'
    ]
];

这是我的controller.php

public $implement = [        
    'Backend\Behaviors\ListController',
    'Backend\Behaviors\FormController',
    'Backend\Behaviors\ReorderController',
    'Backend\Behaviors\RelationController'
];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public $relationConfig = 'config_relation.yaml';

这是我的config_relation.yaml

equipments:
    label: Equipments
    view:
        list:
            columns:
                id:
                    label: ID
                    type: number
                    searchable: true
                    sortable: true
                name:
                    label: Name
                    type: text
                    searchable: true
                    sortable: true
                value:
                    label: Value
                    type: number
                    searchable: true
                    sortable: true
                updated_at:
                    label: Updated
                    type: datetime
                    searchable: true
                    sortable: true
                pivot[quantity]:
                    label: Quantity
                    type: number

    pivot:
        form:
            fields:
                pivot[quantity]:
                    label: Quantity
                    type: number
                    default: 0

所以这是我正确显示关系管理器的后端表单。您会注意到数量字段没有被填写。在我的数据库的第二张图片中,它确实使用以下表格正确填写更新和删除:

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

所以我在四处挖掘并阅读 laravel 文档后解决了这个问题。解决方案是添加pivot => ['quantity']模型关系配置。

public $belongsToMany = [
    'equipments' => [
        'Brandon\Pixelrpg\Models\Equipments',
        'table' => 'brandon_pixelrpg_equipment_inventory',
        'key' => 'inventory',
        'otherKey' => 'equipment',
        'pivot' => ['quantity']
    ]
];
于 2020-04-09T02:21:37.397 回答