目录
▸
入门
简介
安装
1.x升级指南
版本升级
快速开始
开发前必读
主题与颜色
静态资源
视图与自定义页面
多应用(多后台)
JS组件
常见问题
Laravel Octane
▸
数据表格
基本使用
列的使用和扩展
列的显示和扩展
行的使用和扩展
工具栏
树状表格
组合表头
数据来源以及查询条件
关联关系
查询过滤
列过滤器
快捷搜索
规格筛选器
数据导出
快捷创建
行内编辑
事件
字段翻译
数据软删除
头部和脚部
表格异步渲染
▸
数据表单
基本使用
图片/文件上传
字段的使用
字段扩展
数据源
表单弹窗
关联关系
JSON表单
分步表单
表单验证
工具表单
事件以及表单响应
初始化
表单布局
字段翻译
字段动态显示
▸
数据详情
基本使用
字段显示
关联关系
字段显示扩展
初始化
字段翻译
▸
模型树
基本使用
▸
数据仓库
基本使用
▸
动作
基本使用
数据表格
数据表单
数据详情
模型树
▸
多语言
基本使用
▸
开发扩展
扩展基本使用
开发扩展
▸
页面组件
异步加载
图表
数据统计卡片
模态窗(Modal)
工具表单
下拉菜单
单/复选框
选项卡
警告框
提示窗
Markdown
卡片
▸
区块
区块基本使用
▸
动作以及表单响应
动作以及表单响应
▸
权限控制
权限控制
▸
菜单
菜单基本使用
▸
帮助函数
帮助函数
▸
开发工具
基本使用
▸
自定义登陆认证
自定义登录
▸
自定义头部导航
自定义头部导航条
▸
更新日志
BETA版本更新日志
更新日志
内容导航
1.x
2.x
关联关系
创建时间:2024-06-26 13:59:27 / 更新时间:2024-06-26 13:59:27
# 表单关联关系 ### 一对一 `users`表和`profiles`表通过`profiles.user_id`字段生成一对一关联 ```sql CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `profiles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `age` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 对应的数据模分别为: ```php <?php namespace App\Admin\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { public function profile() { return $this->hasOne(Profile::class); } } class Profile extends Model { public function user() { return $this->belongsTo(User::class); } } ``` 对应的数据仓库为: ```php <?php namespace App\Admin\Repositories; use Dcat\Admin\Repositories\EloquentRepository; use User as UserModel; class User extends \Dcat\Admin\Repositories\EloquentRepository { protected $eloquentClass = UserModel::class; } ``` 通过下面的代码可以关联在一个form里面: > {tip} 实例化数据仓库时需要传入关联模型定义的关联名称,相当于主动使用`Eloquent\Model::with`方法。 ```php use App\Admin\Repositories\User; // 注意这里实例化数据仓库`User`时必须传入"profile",否则将无法关联"profiles"表数据 $form = Form::make(User::with('profile'), function (Form $form) { $form->display('id'); $form->text('name'); $form->text('email'); $form->text('profile.age'); $form->text('profile.gender'); $form->datetime('created_at'); $form->datetime('updated_at'); }); ``` 如果你不想使用数据仓库,也可以直接使用模型 ```php use App\Admin\Models\User; // 注意这里是直接使用模型,没有使用数据仓库 $form = Form::make(User::with('profile'), function (Form $form) { $form->display('id'); ... }); ``` ### 一对多 一对多的使用请参考文档[表单字段的使用-一对多](https://learnku.com/docs/dcat-admin/1.x/use-of-fields/8107#onemany) ### 多对多 下面以项目内置的`角色管理`模块的**角色绑定权限**功能为例来演示多对多关联模型的用法 模型`Role` ```php <?php namespace Dcat\Admin\Models; use Dcat\Admin\Traits\HasDateTimeFormatter; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Role extends Model { use HasDateTimeFormatter; /** * 定义你的关联模型. * * @return BelongsToMany */ public function permissions(): BelongsToMany { $pivotTable = 'admin_role_permissions'; // 中间表 $relatedModel = Permission::class; // 关联模型类名 return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id'); } } ``` ```php use Dcat\Admin\Models\Permission; // 实例化数据仓库时传入 permissions,则会自动关联关联模型的数据 // 这里传入 permissions 关联权限模型的数据 $repository = Role::with(['permissions']); return Form::make($repository, function (Form $form) { $form->display('id', 'ID'); $form->text('slug', trans('admin.slug'))->required(); $form->text('name', trans('admin.name'))->required(); // 这里的数据会自动保存到关联模型中 $form->tree('permissions') ->nodes(function () { return (new Permission())->allNodes(); }) ->customFormat(function ($v) { if (!$v) return []; // 这一步非常重要,需要把数据库中查出来的二维数组转化成一维数组 return array_column($v, 'id'); }); ... }); ``` 如果你不想使用数据仓库,也可以直接使用模型 ```php use Dcat\Admin\Models\Role; // 注意这里是直接使用模型,没有使用数据仓库 $form = Form::make(Role::with('permissions'), function (Form $form) { $form->display('id'); ... }); ``` 最终效果如下 ![](http://docs.oneself.icu:10010/storage/markdown/images/1476ce7c7f24af0d3dbc03b6c7785983667bae3391833.png) ### 关联模型名称为驼峰风格 如果你的关联模型名称的命名是**驼峰**风格,那么使用的时候需要转化为**下划线**风格命名 例如 ```php class User extend Model { public function userProfile() { return ...; } } ``` 使用 ```php return Form::make(User::with(['userProfile']), function (Form $form) { ... // 注意这里必须使用下划线风格命名,否则将无法显示编辑数据 $form->text('user_profile.postcode'); $form->text('user_profile.address'); }); ```
上一文章
下一文章
返回顶部
返回主页
返回文档展示页