dcat-admin

dcat-admin


关联关系

<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-&amp;gt;belongsTo(User::class, 'author_id'); } }</code></pre> <p>数据仓库定义为:</p> <pre><code class="language-php">&amp;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-&amp;gt;author(function ($model) { return Show::make($model-&amp;gt;author_id, new User(), function (Show $show) { $show-&amp;gt;resource('/users'); $show-&amp;gt;id(); $show-&amp;gt;name(); $show-&amp;gt;email(); }); });</code></pre> <p>&gt; {tip} 为了能够正常使用这个面板右上角的工具,必须用<code>resource</code>方法设置用户资源的url访问路径。</p> <p>如果你的关联模型还需要有其他的条件查询,则可以参考以下方式</p> <pre><code class="language-php">use App\Models\User; $show-&amp;gt;author(function ($model) { // 模型设置查询条件 $userModel = User::where('state', $model-&amp;gt;state); return Show::make($model-&amp;gt;author_id, $userModel, function (Show $show) { // 设置路由 $show-&amp;gt;resource('/users'); $show-&amp;gt;id(); $show-&amp;gt;name(); $show-&amp;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-&amp;gt;field('author.id', '作者ID'); $show-&amp;gt;field('author.name', '作者名称'); ... });</code></pre> <p>如果你的关联模型名称的命名是<strong>驼峰</strong>风格,那么使用的时候需要转化为<strong>下划线</strong>风格命名</p> <pre><code class="language-php">// 注意这里必须使用下划线风格命名,否则将无法显示编辑数据 $show-&amp;gt;field('user_profile.postcode'); $show-&amp;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-&amp;gt;hasMany(Comment::class); } } class Comment extends Model { }</code></pre> <p>数据仓库定义为:</p> <pre><code class="language-php">&amp;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-&amp;gt;comments(function ($model) { $grid = new Grid(new Comment); $grid-&amp;gt;model()-&amp;gt;where('post_id', $model-&amp;gt;id); // 设置路由 $grid-&amp;gt;resource('comments'); $grid-&amp;gt;id(); $grid-&amp;gt;content()-&amp;gt;limit(10); $grid-&amp;gt;created_at(); $grid-&amp;gt;updated_at(); $grid-&amp;gt;filter(function ($filter) { $filter-&amp;gt;like('content')-&amp;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-&amp;gt;belongsToMany(Permission::class, 'role_permissions', 'role_id', 'permission_id'); } } class Permission extends Model { }</code></pre> <p>数据仓库定义为:</p> <pre><code class="language-php">&amp;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-&amp;gt;permissions(function ($model) { $grid = new Grid(new Permission); $grid-&amp;gt;model()-&amp;gt;join('role_permissions', function ($join) use ($model) { $join-&amp;gt;on('role_permissions.permission_id', 'id') -&amp;gt;where('role_id', '=', $model-&amp;gt;id); }); // 设置路由 $grid-&amp;gt;resource('auth/permissions'); $grid-&amp;gt;id; $grid-&amp;gt;name; $grid-&amp;gt;slug; $grid-&amp;gt;http_path; $grid-&amp;gt;filter(function (Grid\Filter $filter) { $filter-&amp;gt;equal('id')-&amp;gt;width('300px'); }); return $grid; });</code></pre>

页面列表

ITEM_HTML