Skip to main content

Dcat Admin 图片文件上传展示

图片

表格:

# 缩略图展示
$grid->column('logo')->image('', 60, 60);

表单:

# 单图上传  保存完整url:->saveFullUrl()   图片名称随机生成    自动上传    禁止删除  禁止页面删除文件(替换上传)  图片路径调整 
$form->image('logo')->uniqueName()->autoUpload()->retainable()->removable(false)->move(FunctionHelper::imgPath());


public static function imgPath()
{
  return config('admin.upload.directory.image') . '/' . date('Y/m/d');
}

详情:

# 图片展示
$show->field('logo')->image('', 100, 100);

多图

表格

$grid->column('detail_img')->display(function ($detail_img) {
  return explode(';', $detail_img); # 要转换成数组展示
})->image('', 60, 60);


或

$grid->column('reference_material', '参考资料')->display('参考') // 设置按钮名称
  ->expand(function () {
    # 展示已审核头像和已审核照片
    $avatar = '<img data-action="preview-img" src="' . FunctionHelper::getFileCdnUrl($this->user_info->avatar) . '" style="max-width:60px;max-height:60px;cursor:pointer" class="img img-thumbnail">';
    $checked_album = \App\Models\XqAlbum::query()->where(['uid' => $this->uid, 'check_status' => 1])->orderByDesc('id')->limit(10)->get()->toArray();
    foreach ($checked_album as $val) {
      if (!empty($val['img'])) {
        $avatar .= '<img data-action="preview-img" src="' . FunctionHelper::getFileCdnUrl($val['img']) . '" style="max-width:60px;max-height:60px;cursor:pointer" class="img img-thumbnail">';
      }
    }
    return "<div style='padding:20px 20px 20px 20px'>{$avatar}</div>";
  });

表单

$form->multipleImage('detail_img')
  ->customFormat(function ($v) { # 展示,要转换成数组形式
    if (!empty($v[0])) {
      return explode(';', (string)$v[0]);
    }
    return $v;
  })
  ->saveFullUrl()->uniqueName()->autoUpload()->retainable()->removable(false);

或
  
$form->multipleImage('banner')->uniqueName()->autoUpload()->retainable()->removable(false)->required()
  ->saveFullUrl()->uniqueName()->autoUpload()->retainable()->removable(false);

# 注意上面的格式转换,若数据库直接保存json格式,就没必要这样转换了,默认存储的就是数组

详情

和单图一样

# 图片展示
$show->field('logo')->image('', 100, 100);