关联关系
<h1>关联关系</h1>
<h2>一对一</h2>
<p><code>users</code>表和上面的<code>posts</code>表为一对一关联关系,通过<code>posts.author_id</code>字段关联,<code>users</code>和<code>post</code>表结构如下:</p>
<pre><code class="language-sql">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;</code></pre>
<p>模型定义为:</p>
<pre><code class="language-php">class User extends Model
{
}
class Post extends Model
{
public function author()
{
return $this-&gt;belongsTo(User::class, 'author_id');
}
}</code></pre>
<p>数据仓库定义为:</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Repositories;
use Dcat\Admin\Repositories\EloquentRepository;
use User as UserModel;
class User extends EloquentRepository
{
protected $eloquentClass = UserModel::class;
}</code></pre>
<p>那么可以用下面的方式显示<code>post</code>所属的用户的详细:</p>
<pre><code class="language-php">use App\Models\User;
$show-&gt;author(function ($model) {
return Show::make($model-&gt;author_id, new User(), function (Show $show) {
$show-&gt;resource('/users');
$show-&gt;id();
$show-&gt;name();
$show-&gt;email();
});
});</code></pre>
<p>> {tip} 为了能够正常使用这个面板右上角的工具,必须用<code>resource</code>方法设置用户资源的url访问路径。</p>
<p>如果你的关联模型还需要有其他的条件查询,则可以参考以下方式</p>
<pre><code class="language-php">use App\Models\User;
$show-&gt;author(function ($model) {
// 模型设置查询条件
$userModel = User::where('state', $model-&gt;state);
return Show::make($model-&gt;author_id, $userModel, function (Show $show) {
// 设置路由
$show-&gt;resource('/users');
$show-&gt;id();
$show-&gt;name();
$show-&gt;email();
});
});</code></pre>
<h3>简单方式</h3>
<p>如果你只是简单的展示关联表信息,也可以这么写</p>
<pre><code class="language-php">// 如果你用的是模型,可以这样指定关联关系
$model = Post::with('author');
// 如果你用的是数据仓库,可以这样指定关联关系
// $repository = new Post(['author']);
return Show::make($id, $model, function (Show $show) {
$show-&gt;field('author.id', '作者ID');
$show-&gt;field('author.name', '作者名称');
...
});</code></pre>
<p>如果你的关联模型名称的命名是<strong>驼峰</strong>风格,那么使用的时候需要转化为<strong>下划线</strong>风格命名</p>
<pre><code class="language-php">// 注意这里必须使用下划线风格命名,否则将无法显示编辑数据
$show-&gt;field('user_profile.postcode');
$show-&gt;field('user_profile.address');</code></pre>
<h2>一对多</h2>
<p>一对多会以<a href="model-grid.md">数据表格</a>的方式呈现,下面是简单的例子</p>
<p><code>posts</code>表和评论表<code>comments</code>为一对多关系(一条post有多条comments),通过<code>comments.post_id</code>字段关联</p>
<pre><code class="language-sql">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</code></pre>
<p>模型定义为:</p>
<pre><code class="language-php">class Post extends Model
{
public function comments()
{
return $this-&gt;hasMany(Comment::class);
}
}
class Comment extends Model
{
}</code></pre>
<p>数据仓库定义为:</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Repositories;
use Dcat\Admin\Repositories\EloquentRepository;
use Comment as CommentModel;
class Comment extends EloquentRepository
{
protected $eloquentClass = CommentModel::class;
}</code></pre>
<p>那么评论的显示通过下面的代码实现:</p>
<pre><code class="language-php">use App\Admin\Repositories\Comment;
$show-&gt;comments(function ($model) {
$grid = new Grid(new Comment);
$grid-&gt;model()-&gt;where('post_id', $model-&gt;id);
// 设置路由
$grid-&gt;resource('comments');
$grid-&gt;id();
$grid-&gt;content()-&gt;limit(10);
$grid-&gt;created_at();
$grid-&gt;updated_at();
$grid-&gt;filter(function ($filter) {
$filter-&gt;like('content')-&gt;width('300px');
});
return $grid;
});</code></pre>
<p>注意:为了能够正常使用这个数据表格的功能,必须用<code>resource()</code>方法设置<code>comments</code>资源的url访问路径</p>
<h2>多对多</h2>
<p>多对多会以<a href="model-grid.md">数据表格</a>的方式呈现,下面是简单的例子</p>
<p>角色表<code>roles</code>和权限表<code>permissions</code>为多对多关系,通过中间表<code>role_permissions</code>关联</p>
<pre><code class="language-sql">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;</code></pre>
<p>模型定义为:</p>
<pre><code class="language-php">class Role extends Model
{
public function permissions() : BelongsToMany
{
return $this-&gt;belongsToMany(Permission::class, 'role_permissions', 'role_id', 'permission_id');
}
}
class Permission extends Model
{
}</code></pre>
<p>数据仓库定义为:</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Repositories;
use Dcat\Admin\Repositories\EloquentRepository;
use Permission as PermissionModel;
class Permission extends EloquentRepository
{
protected $eloquentClass = PermissionModel::class;
}</code></pre>
<p>那么权限的显示通过下面的代码实现:</p>
<pre><code class="language-php">use App\Admin\Repositories\Permission;
$show-&gt;permissions(function ($model) {
$grid = new Grid(new Permission);
$grid-&gt;model()-&gt;join('role_permissions', function ($join) use ($model) {
$join-&gt;on('role_permissions.permission_id', 'id')
-&gt;where('role_id', '=', $model-&gt;id);
});
// 设置路由
$grid-&gt;resource('auth/permissions');
$grid-&gt;id;
$grid-&gt;name;
$grid-&gt;slug;
$grid-&gt;http_path;
$grid-&gt;filter(function (Grid\Filter $filter) {
$filter-&gt;equal('id')-&gt;width('300px');
});
return $grid;
});</code></pre>