i have a problem in my code where if i do import operation, i always get duplicate rows of imported value. What are the problem in the codes below and how to fix the problem so when i do import operation it won’t get duplicated?
import.php
class SubmissionDetailImport implements ToModel, WithHeadingRow
{
protected $id;
function __construct($id) {
$this->id = $id;
}
public function model(array $row)
{
$spreadsheet = IOFactory::load(request()->file('file'));
$i = 0;
foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case MemoryDrawing::MIMETYPE_PNG :
$extension = 'png';
break;
case MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif';
break;
case MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg';
break;
}
} else {
$zipReader = fopen($drawing->getPath(), 'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader, 1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$myFileName = time() .++$i. '.' . $extension;
$path = storage_path('/app/public/images/detail/');
file_put_contents($path . $myFileName, $imageContents);
SubmissionDetail::create([
'submission_id' => $this->id,
'nama_barang' => $row['name'],
'image_path' => '/public/images/detail/' . $myFileName,
'jumlah' => $row['quantity'],
'harga_satuan' => $row['price'],
'harga_total' => $row['total_price'],
'keterangan' => $row['description'],
]);
}
}
}
importcontroller.php
public function import(Request $request) {
Excel::import(new SubmissionDetailImport($request->id), $request->file('file'));
return redirect()->back()->with("status", ["status" => true, "message" => "Success importing detail"]);
}