首页 > 解决方案 > 将文件上传字段添加到 october cms

问题描述

我想通过添加图像字段来自定义 backend_users。我在引导方法中扩展模型和表单字段,如下所示:

public function boot()
{
    BackendUserModel::extend(function($model) {
        $model->attachOne['image'] = \System\Models\File::class;
    });

    BackendUserController::extendFormFields(function($form, $model, $context) {
        $form->addTabFields([
            'image' => [
                'label' => 'image',
                'type' => 'fileupload',
                'tab' => 'image'
            ],
       ]);
   });
}

它显示错误:

模型“System\Models\File”不包含“image”的定义。

我做错了什么?请帮我。谢谢!

标签: octobercms

解决方案


问题

您的BackendUserController::extendFormFields代码正在扩展您的BackendUserController.

所以要确保有两种形式

  1. 后端用户表单
  2. avatar字段的\System\Models\File

因此,您的代码基本上是image在两种形式中都添加了字段,因此我们收到第二种形式的错误Model 'System\Models\File' does not contain a definition for 'image'.

解决方案

为了避免这种情况,我们只需要adding field通过correct model添加condition.

\Backend\Controllers\Users::extendFormFields(function($form, $model, $context) {

    // Only for the backend User model we need to add this
    if ($model instanceof \Backend\Models\User) { // <- add this condition
        $form->addTabFields([
            'image' => [
                'label' => 'image',
                'type' => 'fileupload',
                'tab' => 'image'
            ],
        ]);
    }
});

现在它应该只image为后端用户模型添加字段,并且您的代码应该可以完美运行。

如有任何疑问,请发表评论。


推荐阅读