两个字段相互覆盖,将值保存在用于Laravel CRUD的Backpack中(spatie / laravel-tags实现问题)?

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

我正在尝试将spatie / laravel标签与用于Laravel的背包一起使用。我定义了2种标签。目前,我已经将spatie / laravel-tags中的Tag模型扩展为MyCategory和MyTag,并添加了全局范围以将两种标签类型分开。这样做的程度是,它将正确显示背包中的当前类别和标签,但是当我尝试保存任何更改时,它只会将条目保存在最后一个字段中,并删除第一个字段中的所有内容。

这是我当前的CRUD字段配置:

        $this->crud->addField([
            'name' => 'categories',
            'label' => 'Categories',
            'type' => 'select2_multiple',
            'tab' => 'Overview',
            'attribute' => 'name',
            'model' => 'App\MyCategory',
            'pivot' => true,
        ]);

        $this->crud->addField([
            'name' => 'tags',
            'label' => 'Tags',
            'type' => 'select2_multiple',
            'tab' => 'Overview',
            'attribute' => 'name',
            'model' => 'App\MyTag',
            'pivot' => true,
        ]);

[当我检查Laravel望远镜时,我看到两个字段都发生了相同的事情。首先,将删除我要保存的项目的所有当前标签(无论类型如何),并添加该字段中的新标签。然后对第二个字段重复此操作,这当然会从第一个字段中删除也应保留的标签。

似乎在我的扩展Tag型号上,GlobalScope在这一部分上并不存在。是否有任何方法可以将范围重新引入背包运行的查询中以正确保存这些标签?

laravel backpack-for-laravel spatie
1个回答
0
投票

在我的CrudController中,我创建了自定义更新和存储功能。请参阅下面的更新示例。这似乎很好。仍然有2个查询正在运行,第二个查询正在撤消第一个查询,但就我的目的而言,这是一个足够好的解决方法,可以使用spatie / laravel-在Backpack中以相同的形式使用相同标签的2个字段标签。

    public function update(UpdateRequest $request)
    {
        $request = request();

        // Merge the values from the two tag fields together into the second field
        $request->merge(['tags' => array_merge((array)$request->input('categories'), (array)$request->input('tags'))]);

        $redirect_location = $this->traitUpdate($request);

        return $redirect_location;
    }
© www.soinside.com 2019 - 2024. All rights reserved.