当前位置:网站首页>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();
边栏推荐
- CEPH operation and maintenance common instructions
- 9、 Beautify tables, forms, and hyperlinks
- sysbench基础介绍
- Introduction to sysbench Basics
- Redis中执行Lua脚本
- 服务器单、双向可调一键互信脚本!
- 小笔记-简单但够用系列_KVM快速入门
- [difficult and miscellaneous diseases] @transitional failure summary
- ACK攻击是什么意思?ACK攻击怎么防御?
- Common regular expressions - tool classes (mobile number, email, QQ, fax)
猜你喜欢

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

Enter a positive integer with no more than 5 digits, and output the last digit in reverse order

Idea remote debugger

Redis (basic) - learning notes

Linux下安裝Mysql【詳細】

看我在Map<String, String>集合中,存入Integer类型数据
![[work details] March 18, 2020](/img/24/a72230daac08e7ec5bd57df08071f8.jpg)
[work details] March 18, 2020

栖霞市住建局和消防救援大队开展消防安全培训

【北邮果园微处理器设计】10 Serial Communication 串口通信笔记

DataBinding使用与原理分析
随机推荐
SQL index learning notes
8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
Redis中执行Lua脚本
Concise course of probability theory and statistics in engineering mathematics second edition review outline
哪些PHP开源作品值得关注
jwt认证协议阐述之——我开了一家怡红院
【深度学习理论】(7) 长短时记忆网络 LSTM
Based on Zeng Shen's explanation, the line segment tree is studied again one
一键部署ceph脚本
MySQL 12th job - Application of stored procedure
MySQL 8th job
DataBinding使用与原理分析
Tape library simple record 1
Grain Mall - distributed Foundation
ISO 26262 - 2 functional safety concept
Vscode environment setup: synchronous configuration
Oracle sqlplus query result display optimization
Enter a positive integer with no more than 5 digits, and output the last digit in reverse order
See how I store integer data in the map < string, string > set
Search engine advanced search method records