将文件上传字段添加到 october cms

Add fileupload fields into 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'
            ],
       ]);
   });
}

并且显示错误:

Model 'System\Models\File' does not contain a definition for 'image'.

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

Problem

您的 BackendUserController::extendFormFields 代码正在扩展您 BackendUserController 上的每一个表单。

所以要确定有2种形式

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

所以, 你的代码基本上是在两种形式中添加 image 字段,所以我们在第二种形式 Model 'System\Models\File' does not contain a definition for 'image'.

中遇到错误

Solution

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

来确保我们是 adding fieldcorrect model
\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'
            ],
        ]);
    }
});

Now it should only add image field for backend user model and your code should work perfectly.

如有疑问请评论。