表格关联关系

一对一

users表和profiles表通过profiles.user_id字段生成一对一关联

  1. CREATE TABLE `users` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  6. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  9. CREATE TABLE `profiles` (
  10. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  11. `user_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  12. `age` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  13. `gender` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  14. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  15. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  16. PRIMARY KEY (`id`)
  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

对应的数据模以及数据仓库分别为:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. public function profile()
  7. {
  8. return $this->hasOne(Profile::class);
  9. }
  10. }
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Profile extends Model
  5. {
  6. public function user()
  7. {
  8. return $this->belongsTo(User::class);
  9. }
  10. }

数据仓库

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\EloquentRepository;
  4. use App\Models\User as UserModel;
  5. class User extends EloquentRepository
  6. {
  7. protected $eloquentClass = UserModel::class;
  8. }

三种关联数据表的方法

通过以下三种方式的代码可以关联profile表数据:

方式一:直接使用数据仓库关联

  1. use App\Admin\Repositories\User;
  2. use Dcat\Admin\Grid;
  3. // 关联 profile 表数据
  4. $grid = Grid::make(User::with(['profile']), function (Grid $grid) {
  5. $grid->id('ID')->sortable();
  6. $grid->name();
  7. $grid->email();
  8. // 显示一对一数据
  9. $grid->column('profile.age');
  10. $grid->column('profile.gender');
  11. $grid->created_at();
  12. $grid->updated_at();
  13. });

方式二:使用Model::with方法关联

  1. use App\Models\User;
  2. use Dcat\Admin\Grid;
  3. // 关联 profile 表数据
  4. $grid = Grid::make(User::with(['profile']), function (Grid $grid) {
  5. $grid->id('ID')->sortable();
  6. ...
  7. });

方式三:使用Grid\Model方法关联

  1. use App\Admin\Repositories\User;
  2. use Dcat\Admin\Grid;
  3. $grid = Grid::make(new User(), function (Grid $grid) {
  4. // 关联 profile 表数据
  5. $grid->model()->with(['profile']);
  6. $grid->id('ID')->sortable();
  7. ...
  8. });

一对多

posts表和comments表通过comments.post_id字段生成一对多关联

  1. CREATE TABLE `posts` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  6. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  9. CREATE TABLE `comments` (
  10. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  11. `post_id` int(10) unsigned NOT NULL,
  12. `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  13. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  14. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  15. PRIMARY KEY (`id`)
  16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

对应的数据模和数据仓库分别为:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Post extends Model
  5. {
  6. public function comments()
  7. {
  8. return $this->hasMany(Comment::class);
  9. }
  10. }
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Comment extends Model
  5. {
  6. public function post()
  7. {
  8. return $this->belongsTo(Post::class);
  9. }
  10. }
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\Post as PostModel;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class Post extends EloquentRepository
  6. {
  7. protected $eloquentClass = PostModel::class;
  8. }
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\Comment as CommentModel;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class Comment extends EloquentRepository
  6. {
  7. protected $eloquentClass = CommentModel::class;
  8. }

同样这里支持上述的三种方式关联数据,限于篇幅这里不再重复写所有用法

Post表格

  1. use App\Admin\Repositories\Post;
  2. // 关联 comment 表数据
  3. $grid = Grid::make(Post::with(['comments']), function (Grid $grid) {
  4. $grid->id('id')->sortable();
  5. $grid->title();
  6. $grid->content();
  7. $grid->comments('评论数')->display(function ($comments) {
  8. $count = count($comments);
  9. return "<span class='label label-warning'>{$count}</span>";
  10. });
  11. $grid->created_at();
  12. $grid->updated_at();
  13. });

Comment表格

  1. use App\Admin\Repositories\Comment;
  2. // 关联 post 表数据
  3. $grid = new Grid(Comment::with(['post']));
  4. $grid->column('id');
  5. $grid->column('post.title');
  6. $grid->column('content');
  7. $grid->created_at()->sortable();
  8. $grid->updated_at();

多对多

usersroles表通过中间表role_users产生多对多关系

  1. CREATE TABLE `users` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `username` varchar(190) COLLATE utf8_unicode_ci NOT NULL,
  4. `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  5. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  6. `created_at` timestamp NULL DEFAULT NULL,
  7. `updated_at` timestamp NULL DEFAULT NULL,
  8. PRIMARY KEY (`id`),
  9. UNIQUE KEY `users_username_unique` (`username`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  11. CREATE TABLE `roles` (
  12. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  13. `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  14. `slug` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  15. `created_at` timestamp NULL DEFAULT NULL,
  16. `updated_at` timestamp NULL DEFAULT NULL,
  17. PRIMARY KEY (`id`),
  18. UNIQUE KEY `roles_name_unique` (`name`)
  19. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  20. CREATE TABLE `role_users` (
  21. `role_id` int(11) NOT NULL,
  22. `user_id` int(11) NOT NULL,
  23. `created_at` timestamp NULL DEFAULT NULL,
  24. `updated_at` timestamp NULL DEFAULT NULL,
  25. KEY `role_users_role_id_user_id_index` (`role_id`,`user_id`)
  26. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

对应的数据模和数据仓库分别为:

User 模型

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. public function roles()
  7. {
  8. return $this->belongsToMany(Role::class);
  9. }
  10. }

Role 模型

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Role extends Model
  5. {
  6. public function users()
  7. {
  8. return $this->belongsToMany(User::class);
  9. }
  10. }

数据仓库

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\User as UserModel;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class User extends EloquentRepository
  6. {
  7. protected $eloquentClass = UserModel::class;
  8. }

同样这里支持上述的三种方式关联数据,限于篇幅这里不再重复写所有用法

  1. use App\Admin\Repositories\User;
  2. // 关联 role 表数据
  3. $grid = Grid::make(User::with('roles'), function (Grid $grid) {
  4. $grid->id('ID')->sortable();
  5. $grid->username();
  6. $grid->name();
  7. $grid->roles()->pluck('name')->label();
  8. $grid->created_at();
  9. $grid->updated_at();
  10. });