首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel :如何从excel文件中导入图像并将其存储到数据库中?

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

Stack Overflow用户
提问于 2022-06-16 08:47:14
回答 1查看 931关注 0票数 0

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

下面是excel文件的示例:

以下是我的导入代码:

代码语言:javascript
运行
复制
<?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,但是由于我的知识有限,我在那个网站上什么都找不到,我需要帮助

EN

回答 1

Stack Overflow用户

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

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

代码语言:javascript
运行
复制
$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();

                    $array_row[] = $value;
                }

                $array_rows_data[] = $array_row;
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72642981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档