目录
▸
入门
简介
安装
1.x升级指南
版本升级
快速开始
开发前必读
主题与颜色
静态资源
视图与自定义页面
多应用(多后台)
JS组件
常见问题
Laravel Octane
▸
数据表格
基本使用
列的使用和扩展
列的显示和扩展
行的使用和扩展
工具栏
树状表格
组合表头
数据来源以及查询条件
关联关系
查询过滤
列过滤器
快捷搜索
规格筛选器
数据导出
快捷创建
行内编辑
事件
字段翻译
数据软删除
头部和脚部
表格异步渲染
▸
数据表单
基本使用
图片/文件上传
字段的使用
字段扩展
数据源
表单弹窗
关联关系
JSON表单
分步表单
表单验证
工具表单
事件以及表单响应
初始化
表单布局
字段翻译
字段动态显示
▸
数据详情
基本使用
字段显示
关联关系
字段显示扩展
初始化
字段翻译
▸
模型树
基本使用
▸
数据仓库
基本使用
▸
动作
基本使用
数据表格
数据表单
数据详情
模型树
▸
多语言
基本使用
▸
开发扩展
扩展基本使用
开发扩展
▸
页面组件
异步加载
图表
数据统计卡片
模态窗(Modal)
工具表单
下拉菜单
单/复选框
选项卡
警告框
提示窗
Markdown
卡片
▸
区块
区块基本使用
▸
动作以及表单响应
动作以及表单响应
▸
权限控制
权限控制
▸
菜单
菜单基本使用
▸
帮助函数
帮助函数
▸
开发工具
基本使用
▸
自定义登陆认证
自定义登录
▸
自定义头部导航
自定义头部导航条
▸
更新日志
BETA版本更新日志
更新日志
内容导航
1.x
2.x
关联关系
创建时间:2024-06-26 10:47:50 / 更新时间:2024-06-26 10:47:50
# 表格关联关系 ### 一对一 `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\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { public function profile() { return $this->hasOne(Profile::class); } } ``` ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Profile extends Model { public function user() { return $this->belongsTo(User::class); } } ``` 数据仓库 ```php <?php namespace App\Admin\Repositories; use Dcat\Admin\Repositories\EloquentRepository; use App\Models\User as UserModel; class User extends EloquentRepository { protected $eloquentClass = UserModel::class; } ``` <a name="relation"></a> #### 三种关联数据表的方法 通过以下三种方式的代码可以关联`profile`表数据: 方式一:直接使用数据仓库关联 ```php use App\Admin\Repositories\User; use Dcat\Admin\Grid; // 关联 profile 表数据 $grid = Grid::make(new User(['profile']), function (Grid $grid) { $grid->id('ID')->sortable(); $grid->name(); $grid->email(); // 显示一对一数据 $grid->column('profile.age'); $grid->column('profile.gender'); $grid->created_at(); $grid->updated_at(); }); ``` 方式二:使用`Model::with`方法关联 ```php use App\Models\User; use Dcat\Admin\Grid; // 关联 profile 表数据 $grid = Grid::make(User::with(['profile']), function (Grid $grid) { $grid->id('ID')->sortable(); ... }); ``` 方式三:使用`Grid\Model`方法关联 ```php use App\Admin\Repositories\User; use Dcat\Admin\Grid; $grid = Grid::make(new User(), function (Grid $grid) { // 关联 profile 表数据 $grid->model()->with(['profile']); $grid->id('ID')->sortable(); ... }); ``` ### 一对多 `posts`表和`comments`表通过`comments.post_id`字段生成一对多关联 ```sql CREATE TABLE `posts` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `content` 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 `comments` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `post_id` int(10) unsigned NOT NULL, `content` 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\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function comments() { return $this->hasMany(Comment::class); } } ``` ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Comment extends Model { public function post() { return $this->belongsTo(Post::class); } } ``` ```php <?php namespace App\Admin\Repositories; use App\Models\Post as PostModel; use Dcat\Admin\Repositories\EloquentRepository; class Post extends EloquentRepository { protected $eloquentClass = PostModel::class; } ``` ```php <?php namespace App\Admin\Repositories; use App\Models\Comment as CommentModel; use Dcat\Admin\Repositories\EloquentRepository; class Comment extends EloquentRepository { protected $eloquentClass = CommentModel::class; } ``` 同样这里支持上述的[三种方式关联数据](#relation),限于篇幅这里不再重复写所有用法 Post表格 ```php use App\Admin\Repositories\Post; // 关联 comment 表数据 $grid = Grid::make(new Post(['comments']), function (Grid $grid) { $grid->id('id')->sortable(); $grid->title(); $grid->content(); $grid->comments('评论数')->display(function ($comments) { $count = count($comments); return "<span class='label label-warning'>{$count}</span>"; }); $grid->created_at(); $grid->updated_at(); }); ``` Comment表格 ```php use App\Admin\Repositories\Comment; // 关联 post 表数据 $grid = new Grid(new Comment(['post'])); $grid->column('id'); $grid->column('post.title'); $grid->column('content'); $grid->created_at()->sortable(); $grid->updated_at(); ``` ### 多对多 `users`和`roles`表通过中间表`role_users`产生多对多关系 ```sql CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(190) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `users_username_unique` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CREATE TABLE `roles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `slug` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `roles_name_unique` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CREATE TABLE `role_users` ( `role_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, KEY `role_users_role_id_user_id_index` (`role_id`,`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ``` 对应的数据模和数据仓库分别为: User 模型 ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { public function roles() { return $this->belongsToMany(Role::class); } } ``` Role 模型 ```php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Role extends Model { public function users() { return $this->belongsToMany(User::class); } } ``` 数据仓库 ```php <?php namespace App\Admin\Repositories; use App\Models\User as UserModel; use Dcat\Admin\Repositories\EloquentRepository; class User extends EloquentRepository { protected $eloquentClass = UserModel::class; } ``` 同样这里支持上述的[三种方式关联数据](#relation),限于篇幅这里不再重复写所有用法 ```php use App\Admin\Repositories\User; // 关联 role 表数据 $grid = Grid::make(new User('roles'), function (Grid $grid) { $grid->id('ID')->sortable(); $grid->username(); $grid->name(); $grid->roles()->pluck('name')->label(); $grid->created_at(); $grid->updated_at(); }); ```
上一文章
下一文章
返回顶部
返回主页
返回文档展示页