我想将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,但是由于我的知识有限,我在那个网站上什么都找不到,我需要帮助
发布于 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();
$array_row[] = $value;
}
$array_rows_data[] = $array_row;
}
https://stackoverflow.com/questions/72642981
复制相似问题