octobercms - 将文件上传字段添加到 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”的定义。
我做错了什么?请帮我。谢谢!
解决方案
问题
您的BackendUserController::extendFormFields
代码正在扩展您的BackendUserController
.
所以要确保有两种形式
- 后端用户表单
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
为后端用户模型添加字段,并且您的代码应该可以完美运行。
如有任何疑问,请发表评论。
推荐阅读
- python - Python随机数组打印选择的变量
- android - Android:在 DAO 中使用 Room 数据库和 LiveData 的干净架构
- react-native - React native - iOs - Push Notification 在 iOs <= 10.3.3 及更低版本上不起作用
- facebook-graph-api - 图形 API manage_pages 和 publish_pages 权限从访问令牌的范围中消失
- python - 根据变量值生成进度条
- java - 从方法返回一个字符串,而不是打印到 System.out
- android - Android - Gradle - 如何下载包含的 jar 的传递依赖项
- typescript - Angular 6 依赖服务调用
- smtp - SMTP 错误:无法连接到服务器:网络无法访问 (101) SMTP 连接失败
- javascript - 使用Break进行循环无限循环?反应式