关联关系

未匹配的标注

关联关系

一对一

users表和上面的posts表为一对一关联关系,通过posts.author_id字段关联,userspost表结构如下:

CREATE TABLE `posts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `author_id` int(10) unsigned NOT NULL ,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `rate` int(255) COLLATE utf8_unicode_ci NOT NULL,
  `release_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `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 `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;

模型定义为:

class User extends Model
{
}

class Post extends Model
{
    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }
}

数据仓库定义为:

<?php

namespace App\Admin\Repositories;

use Dcat\Admin\Repositories\EloquentRepository;
use User as UserModel;

class User extends EloquentRepository
{
    protected $eloquentClass = UserModel::class;
}

那么可以用下面的方式显示post所属的用户的详细:

use App\Models\User;

$show->relation('author', function ($model) {
    return Show::make($model->author_id, new User(), function (Show $show) {
        // 设置路由
        $show->setResource('/users');

        $show->id();
        $show->name();
        $show->email();
    });
});

为了能够正常使用这个面板右上角的工具,必须用setResource方法设置用户资源的url访问路径。

如果你的关联模型还需要有其他的条件查询,则可以参考以下方式

use App\Models\User;

$show->relation('author', function ($model) {
    // 模型设置查询条件
    $userModel = User::where('state', $model->state);

    return Show::make($model->author_id, $userModel, function (Show $show) {
        // 设置路由
        $show->setResource('/users');

        $show->id();
        $show->name();
        $show->email();
    });
});

简单方式

如果你只是简单的展示关联表信息,也可以这么写

// 如果你用的是模型,可以这样指定关联关系
$model = Post::with('author');

// 如果你用的是数据仓库,可以这样指定关联关系
// $repository = new Post(['author']);

return Show::make($id, $model, function (Show $show) {
    $show->field('author.id', '作者ID');
    $show->field('author.name', '作者名称');

    ...
});

如果你的关联模型名称的命名是驼峰风格,那么使用的时候需要转化为下划线风格命名

// 注意这里必须使用下划线风格命名,否则将无法显示编辑数据
$show->field('user_profile.postcode');
$show->field('user_profile.address');

一对多

一对多会以数据表格的方式呈现,下面是简单的例子

posts表和评论表comments为一对多关系(一条post有多条comments),通过comments.post_id字段关联

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

模型定义为:

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

class Comment extends Model
{
}

数据仓库定义为:

<?php

namespace App\Admin\Repositories;

use Dcat\Admin\Repositories\EloquentRepository;
use Comment as CommentModel;

class Comment extends EloquentRepository
{
    protected $eloquentClass = CommentModel::class;
}

那么评论的显示通过下面的代码实现:

use App\Admin\Repositories\Comment;

$show->relation('comments', function ($model) {
    $grid = new Grid(new Comment);

    $grid->model()->where('post_id', $model->id);

    // 设置路由
    $grid->setResource('comments');

    $grid->id();
    $grid->content()->limit(10);
    $grid->created_at();
    $grid->updated_at();

    $grid->filter(function ($filter) {
        $filter->like('content')->width('300px');
    });

    return $grid;
});

注意:为了能够正常使用这个数据表格的功能,必须用setResource()方法设置comments资源的url访问路径

多对多

多对多会以数据表格的方式呈现,下面是简单的例子

角色表roles和权限表permissions为多对多关系,通过中间表role_permissions关联

CREATE TABLE `roles` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `admin_roles_name_unique` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `permissions` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `http_method` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `http_path` text COLLATE utf8mb4_unicode_ci,
  `order` int(11) NOT NULL DEFAULT '0',
  `parent_id` int(11) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `admin_permissions_name_unique` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `role_permissions` (
  `role_id` int(11) NOT NULL,
  `permission_id` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  KEY `admin_role_permissions_role_id_permission_id_index` (`role_id`,`permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

模型定义为:

class Role extends Model
{
  public function permissions() : BelongsToMany
  {
      return $this->belongsToMany(Permission::class, 'role_permissions', 'role_id', 'permission_id');
  }

}

class Permission extends Model
{
}

数据仓库定义为:

<?php

namespace App\Admin\Repositories;

use Dcat\Admin\Repositories\EloquentRepository;
use Permission as PermissionModel;

class Permission extends EloquentRepository
{
    protected $eloquentClass = PermissionModel::class;
}

那么权限的显示通过下面的代码实现:

use App\Admin\Repositories\Permission;

$show->relation('permissions', function ($model) {
    $grid = new Grid(new Permission);

    $grid->model()->join('role_permissions', function ($join) use ($model) {
        $join->on('role_permissions.permission_id', 'id')
            ->where('role_id', '=', $model->id);
    });

    // 设置路由
    $grid->setResource('auth/permissions');

    $grid->id;
    $grid->name;
    $grid->slug;
    $grid->http_path;

    $grid->filter(function (Grid\Filter $filter) {
        $filter->equal('id')->width('300px');
    });

    return $grid;
});

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:1