我有一个问题要跳过有关使用 Maatwebsite/Laravel-Excel 包导入 excel laravel 的行。
我从互联网上尝试了很多方法,但仍然没有奏效。
这是我的 Controller 代码
if ($request->hasFile('file')) {
$import = new HsatuImport();
$file = $request->file('file'); //GET FILE
// config(['excel.import.startRow' => 2]);
Excel::import($import, $file)->limit(false, 2); //IMPORT FILE
return redirect()->route('admin.hsatus.upload')->withFlashSuccess('Upload Berhasil '.$import->getRowCount().' Data');
} else {
return redirect()->route('admin.hsatus.upload')->withFlashSuccess('Upload Gagal');
}
这是我的导入代码
<?php
namespace App\Imports;
use App\Models\Hsatu;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\ToModel;
class HsatuImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
private $rows = 0;
public function model(array $row)
{
++$this->rows;
return new Hsatu([
'region' => @$row[0],
'nomor_faktur' => @$row[1],
'nomor_rangka' => @$row[2],
'kode_mesin' => @$row[3]
]);
}
public function headingRow(): int
{
return 1;
}
public function getRowCount(): int
{
return $this->rows;
}
}
最佳答案
当已经存在 Eloquent 模型时,我遇到了跳过的问题。所以在我的代码下面:
public function model(array $row)
{
$name = $row['name'];
if (!Category::where('name', $name)->exists())
return new Category([
'name' => $name
]);
}
仅当给定语句为真时,我才返回模型实例。
关于php - 如何跳过有关使用 Maatwebsite/Laravel-Excel 导入 excel laravel 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60909750/