简体   繁体   English

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

[英]Laravel Import: How to import image from the excel file and store it to the database?

i want to store all the data in an excel file into the database however, the excel file contains an image and i can't figure how to properly store it, im using Maatwebsite/excel package.我想将excel文件中的所有数据存储到数据库中,但是excel文件包含一个图像,我不知道如何正确存储它,我使用的是Maatwebsite/excel包。

Here are the sample of the excel file:这是excel文件的示例: 在此处输入图像描述

here are my import code:这是我的导入代码:

<?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'],
            ]);
        }
    }
}

I found a similar question and (maybe) solution here https://laracasts.com/discuss/channels/laravel/cant-import-images-using-laravel-excel however due to my limited knowledge, i can't fathom anything on that website, i need help我在这里找到了一个类似的问题和(也许)解决方案https://laracasts.com/discuss/channels/laravel/cant-import-images-using-laravel-excel但是由于我的知识有限,我无法理解任何内容那个网站,我需要帮助

I used to use PhpSpreadsheet .我曾经使用PhpSpreadsheet This is an exemple of code.这是一个代码示例。

$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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM