当前位置:网站首页>laravel 使用 OSS时拼接 完整路径 及 laravel-admin 的多图片上传
laravel 使用 OSS时拼接 完整路径 及 laravel-admin 的多图片上传
2022-06-26 10:04:00 【siner.li】
在模型中添加(方法一)
protected static function boot()
{
parent::boot();
static::created(function ($modal) {
// 获取添加之后的字段值
$url = $modal->getAttribute('gwt');
$str = "https://www.baidu.com";
$new_url = $str . '/' . $url;
$modal->gwt = $new_url;
$modal->save();
});
}
在 form 中添加(方法二)
//把id通过隐藏域传入
$form->hidden('id');
$form->image('img', '图片')
$form->saved(function (Form $form){
// 判断是否有 https://www.baidu.com 字段
if(strpos($form->model()->img, 'https://www.baidu.com') === false){
$url = $form->model()->img;
$img = "https://www.baidu.com/".$url;
// 通过 use 引入 Contracts 模型
$info = Contracts::find($form->model()->id);
$info->img = $img;
$info->save();
}
});
上面时参考别的方法, 但是没有成功, 然后加了点代码.
使用 laravel-admin 多图/文件上传
拼接路径 – (不删图片)
// 模型中添加, setImgAttribute中间的Img即为字段名
public function setImgAttribute($value)
{
// 获取数组最后一个元素
$value[count($value)-1] = 'https://www.baidu.com/' . $value[count($value)-1];
// 限制 3 张图片
if (count($value) > 3) {
array_shift($value);
}
$this->attributes['img'] = implode(',', $value);
}
// getImgAttribute中间的Img即为字段名
public function getImgAttribute($value)
{
return explode(',', $value);
}
// 在 form 中使用 , 这里的 img 值为字段名
$form->multipleImage('img', '缩略图上传');
// 图片轮播
$grid->column('img', '图片')->carousel();
(删图片)
// 模型中添加, setImgAttribute中间的Img即为字段名
public function setImgAttribute($value)
{
if (count($value) == 0) {
$this->attributes['img'] = '';
return;
}
// 限制 3 张图片
if (count($value) > 3) {
array_shift($value);
}
// 获取数组每个元素
foreach ($value as &$v) {
if (strpos($v, 'https://www.baidu.com/')) === false) {
$v = 'https://www.baidu.com/' . $v;
}
}
$this->attributes['img'] = implode(',', $value);
}
// getImgAttribute中间的Img即为字段名
public function getImgAttribute($value)
{
return explode(',', $value);
}
// 在 form 中使用 , 这里的 img 值为字段名
$form->multipleImage('img', '缩略图上传')->removable();
// 图片轮播
$grid->column('img', '图片')->carousel();
边栏推荐
- Common interview questions of binary tree
- Tape library simple record 1
- mysql性能监控和sql语句
- RDB persistence validation test
- Getting started with postman
- JS reverse | four libraries and one platform response data encryption
- Quantitative investment learning - Introduction to classic books
- Alibaba cloud OSS - object storage service (tool)
- [echart] II. User manual and configuration item reading notes
- 用同花顺手机炒股是安全的吗?如何用同花顺炒股
猜你喜欢

Postman入门教程

開發者,微服務架構到底是什麼?

Linux下安裝Mysql【詳細】

3、 Linked list exercise

Query online users and forced withdrawal users based on oauth2

ACK攻击是什么意思?ACK攻击怎么防御?

OpenCV图像处理-灰度处理

Redis knowledge mind map

Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d

Easyexcel - Excel read / write tool
随机推荐
使用‘百家饭’自动生成API调用:JS部分进展(二)
Which PHP open source works deserve attention
nacos2.x.x启动报错信息Error creating bean with name ‘grpcClusterServer‘;
Cereals Mall - Distributed Advanced
工作汇报(2)
Recent work report
Some problems of feign transferring multipartfile
Redis (IV) redis association table caching
MySQL seventh job - update data
April 13, 2021 interview with beaver family
二叉树常见面试题
服务器单、双向可调一键互信脚本!
互联网对抗神器之漏洞扫描与反渗透
Progressive web application PWA is the future of application development
jwt认证协议阐述之——我开了一家怡红院
MySQL backup and restore command
QT连接MySql数据查询失败
See how I store integer data in the map < string, string > set
Moore vote, leetcode169, leetcode229
Function run time