Laravel Excel 条件化插入图片的正确实现方式
技术百科
花韻仙語
发布时间:2026-01-25
浏览: 次 在 laravel excel 导出中,可通过 `withdrawings` 接口动态控制图片是否插入:关键是在 `drawings()` 方法中根据条件返回 `drawing` 实例或 `null`,而非无条件创建对象,从而避免“file not found”等异常。
在使用 laravel-excel 进行导出时,若需按业务逻辑条件性插入图片(例如仅当 $this->semnat === 1 且图片路径有效时才渲染签名图),直接在 drawings() 方法中构造 Drawing 对象但未校验前置条件,会导致 Excel Writer 尝试加载空或不存在的路径,最终抛出 File not found! 错误。
正确的做法是:将图片实例的创建与返回完全包裹在条件判断内,并确保路径存在、可读。以下是推荐实现:
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
public function drawings()
{
// ✅ 双重校验:开关开启 + 图片路径非空 + 文件实际存在
if ($this->semnat === 1
&& $this->imgPath
&& file_exists(storage_path('app/public/' . $this->imgPath))) {
$drawing = new Drawing();
$draw
ing->setName('Semnatura');
$drawing->setDescription('This is my logo');
$drawing->setHeight(100);
$drawing->setCoordinates('F3'); // 插入到 F3 单元格
$drawing->setPath(storage_path('app/public/' . $this->imgPath));
return $drawing;
}
// ❌ 必须显式返回 null;返回空数组、false 或不返回均会触发异常
return null;
}⚠️ 重要注意事项:
- drawings() 方法必须返回 null 或单个 Drawing 实例(不支持返回数组);
- 若返回非 null 非 Drawing 类型值(如 [], false, ''),laravel-excel 内部会尝试调用 getPath() 导致报错;
- 建议增加 file_exists() 检查,避免因文件被删除或路径错误导致导出中断;
- WithDrawings 接口本身不提供“禁用”机制,因此条件逻辑必须在 drawings() 方法内完成,无需拆分多个导出类,彻底避免代码重复。
通过该方式,你可在同一导出类中灵活控制图片渲染,兼顾可维护性与健壮性。
# 是在
# 多个
# excel
# 可通过
# 而非
# app
# 不支持
# 时才
# win
# go
# 对象
# 接口
# 报错
# this
# NULL
# 抛出
# php
# office
# laravel
# 或不
# 你可
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Mac如何修改Hosts文件?(本地开发与屏蔽网站
- MAC怎么一键隐藏桌面所有图标_MAC极简模式切换
- Win11怎么清理C盘虚拟内存_Win11清理虚拟
- c++怎么使用std::tuple存储多元组数据_
- Win11怎么查看电脑配置_Win11硬件配置详细
- c++的mutex和lock_guard如何使用
- 如何在Golang中实现并发消息队列消费者_Gol
- Win11如何设置系统语言_Win11系统语言切换
- 手机php文件怎么变成mp4_安卓苹果打开php转
- Drupal 中 HTML 链接被双重转义导致渲染
- 如何在Golang中配置代码格式化工具_使用gof
- Win11搜索不到蓝牙耳机怎么办 Win11蓝牙驱
- Win10怎样卸载iTunes_Win10卸载iT
- Win11怎么设置鼠标宏_Win11鼠标按键自定义
- C#如何序列化对象为XML XmlSerializ
- Python代码测试策略_质量保障解析【教程】
- Win11怎么设置开机密码_Windows11账户
- 如何在Golang中实现服务熔断与限流_Golan
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- Win10如何更改网络连接_Windows10以太
- Mac如何将HEIC图片格式转为JPG_Mac批量
- Win10怎么设置开机密码_Windows10账户
- Win11如何更新显卡驱动 Win11检查和安装设
- 如何优化Golang内存分配与GC调度_Golan
- MAC怎么设置程序窗口永远最前_MAC窗口置顶插件
- 如何在 Django 中修改用户密码后保持会话不丢
- windows如何备份注册表_windows导出和
- 如何使用正则表达式批量替换重复的“-”模式为固定字
- mac怎么退出id_MAC退出iCloud账号与A
- C++中的constexpr和const有什么区别
- Win11怎么修改DNS服务器 Win11设置DN
- Win11如何添加/删除输入法 Win11切换中英
- php怎么下载安装并配置环境变量_命令行调用PHP
- Python解释执行模型_字节码流程说明【指导】
- Win10怎样卸载TeamViewer_Win10
- Win11怎么关闭透明效果_Windows11辅助
- php485读数据时阻塞怎么办_php485非阻塞
- Linux如何安装Golang环境_Linux下G
- 用Python构建微服务架构实践_FastAPI与
- Python数据抓取合法性_合规说明【指导】
- Windows电脑如何进入安全模式?(多种按键方法
- Win11怎么设置DNS服务器_Windows11
- PHP 中如何在函数内持久修改引用变量所指向的目标
- Windows怎样关闭Edge新标签页广告_Win
- Win11怎么格式化U盘_Win11系统U盘格式化
- Linux怎么实现内网穿透_Linux安装Frp客
- Windows系统时间服务错误_W32Time服务
- Windows11怎样开启游戏模式_Windows
- 本地php环境打开php文件直接下载_浏览器解析p
- Windows10如何更改鼠标图标_Win10鼠标


QQ客服