Untitled
unknown
php
a year ago
1.0 kB
7
Indexable
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Illuminate\Support\Collection;
use App\Models\YourModel;
class YourChunkImport implements ToCollection, WithHeadingRow, WithChunkReading
{
private $terminate = false;
public function collection(Collection $rows)
{
if ($this->terminate) {
return;
}
$allEmpty = true;
foreach ($rows as $row) {
if (!$this->isEmptyRow($row)) {
$allEmpty = false;
YourModel::create([
'field1' => $row['field1'],
'field2' => $row['field2'],
]);
}
}
if ($allEmpty) {
$this->terminate = true;
}
}
public function chunkSize(): int
{
return 1000;
}
private function isEmptyRow($row)
{
return empty($row['field1']) && empty($row['field2']);
}
}
Editor is loading...
Leave a Comment