相关文章推荐
奔跑的骆驼  ·  结合使用 Oracle Database ...·  3 天前    · 
快乐的卤蛋  ·  Array.prototype.slice( ...·  1 年前    · 
飘逸的荔枝  ·  如何在Pandas中筛选具有多级列头的列?·  2 年前    · 
道上混的西红柿  ·  java - 为 FileWriter ...·  2 年前    · 
愉快的黄豆  ·  C++实现string存取二进制数据的方法_ ...·  2 年前    · 
Code  ›  Laravel :如何从excel文件中导入图像并将其存储到数据库中?开发者社区
数据库 电子表格 数据库文件
https://cloud.tencent.com/developer/ask/sof/107050148
痛苦的米饭
2 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 Laravel :如何从excel文件中导入图像并将其存储到数据库中?

Stack Overflow用户
提问于 2022-06-16 08:47:14
EN

我想将excel文件中的所有数据存储到数据库中,然而,excel文件包含一个图像,我无法理解如何正确地存储它,我使用Maatwebsite/excel包。

下面是excel文件的示例:

​

以下是我的导入代码:

<?php
namespace App\Imports;
use App\SubmissionDetail;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class SubmissionDetailImport implements OnEachRow, WithHeadingRow
    protected $id;
    function __construct($id) {
            $this->id = $id;
    public function onRow(Row $row)
        $row = $row->toArray();
        $submissionDetails = SubmissionDetail::firstOrCreate(
            ['submission_id' => $this->id, 'nama_barang' => $row['nama_barang']],
                'submission_id' => $this->id,
                'nama_barang' => $row['nama_barang'],
                'image_path' => $row['image_path'],
                'jumlah' => $row['jumlah'],
                'harga_satuan' => $row['harga_satuan'],
                'harga_total' => $row['harga_total'],
                'keterangan' => $row['keterangan'],
        if (! $submissionDetails->wasRecentlyCreated) {
            $submissionDetails->update([
                'image_path' => $row['image_path'],
                'jumlah' => $row['jumlah'],
                'harga_satuan' => $row['harga_satuan'],
                'harga_total' => $row['harga_total'],
                'keterangan' => $row['keterangan'],
}

我在这里找到了一个类似的问题,并且(也许)解决了 https://laracasts.com/discuss/channels/laravel/cant-import-images-using-laravel-excel ,但是由于我的知识有限,我在那个网站上什么都找不到,我需要帮助

1 931 0 票数 0
EN
php
excel
laravel
image
import

回答 1

Stack Overflow用户

发布于 2022-06-17 11:07:43

我以前经常使用 PhpSpreadsheet 。这是一个代码示例。

$reader      = new Xls();
$spreadsheet = $reader->load(public_path(''));
$sheet       = $spreadsheet->getActiveSheet();
//getting the data from xls
$array_rows_data    = array();
$highestRow         = $sheet->getHighestRow();
$highestColumn      = $sheet->getHighestColumn();
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
for ($row = 2; $row <= $highestRow; ++$row) {
                $array_row = array();
                for ($col = 1; $col <= 56; ++$col) {
                    $value = $sheet->getCellByColumnAndRow($col, $row)->getValue();
 
推荐文章
奔跑的骆驼  ·  结合使用 Oracle Database 11g 和 Python
3 天前
快乐的卤蛋  ·  Array.prototype.slice() - JavaScript | MDN
1 年前
飘逸的荔枝  ·  如何在Pandas中筛选具有多级列头的列?
2 年前
道上混的西红柿  ·  java - 为 FileWriter 设置编码为 UTF-8 - SegmentFault 思否
2 年前
愉快的黄豆  ·  C++实现string存取二进制数据的方法_光阴迷客的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号