数据详情基本使用

Dcat\Admin\Show用来显示数据详情,先来个例子,数据库中有posts表:

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

对应的数据模型为App\Models\Post,数据仓库为App\Admin\Repositories\Post,下面的代码可以显示posts表的数据详情:

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Admin\Repositories\Post;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Admin;
  8. class PostController extends Controller
  9. {
  10. public function show($id, Content $content)
  11. {
  12. return $content->header('Post')
  13. ->description('详情')
  14. ->body(Show::make($id, new Post(), function (Show $show) {
  15. $show->id('ID');
  16. $show->title('标题');
  17. $show->content('内容');
  18. $show->rate();
  19. $show->created_at();
  20. $show->updated_at();
  21. $show->release_at();
  22. }));
  23. }
  24. }

基本使用方法

HTML内容转义

为了防止XSS攻击, 默认输出的内容都会使用HTML转义,如果你不想转义输出HTML,可以调用unescape方法:

  1. $show->avatar()->unescape()->as(function ($avatar) {
  2. return "<img src='{$avatar}' />";
  3. });

设置字段宽度

字段宽度默认值为“3”,可以设置1-12之间的数字。

  1. $show->created_at->width(4);

修改面板的样式和标题

  1. $show->panel()
  2. ->style('danger')
  3. ->title('post基本信息...');

style的取值可以是primary、info、danger、warning、default

面板工具设置

面板右上角默认有三个按钮编辑、删除、列表,可以分别用下面的方式关掉它们:

  1. $show->panel()
  2. ->tools(function ($tools) {
  3. $tools->disableEdit();
  4. $tools->disableList();
  5. $tools->disableDelete();
  6. // 显示快捷编辑按钮
  7. $tools->showQuickEdit();
  8. });

自定义复杂工具按钮

请参考文档数据详情动作

多列布局

使用

{tip} Since v1.3.4

  1. $show->row(function (Show\Row $show) {
  2. $show->width(3)->id;
  3. $show->width(3)->name;
  4. $show->width(5)->email;
  5. });
  6. $show->row(function (Show\Row $show) {
  7. $show->width(5)->email_verified_at;
  8. $show->created_at;
  9. $show->updated_at;
  10. });
  11. $show->row(function (Show\Row $show) {
  12. $show->width(3)->field('profile.first_name');
  13. $show->field('profile.last_name');
  14. $show->width(3)->field('profile.postcode');
  15. });

效果 数据详情基本使用 - 图1