Add job for function upload record to csv
This commit is contained in:
52
app/Jobs/ImportCSVFileJob.php
Normal file
52
app/Jobs/ImportCSVFileJob.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use App\Models\Word;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ImportCSVFileJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $filepath;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct($filepath)
|
||||
{
|
||||
$this->filepath = $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$mapping = [
|
||||
'name' => 0, 'translation' => 1,
|
||||
];
|
||||
$fileStream = fopen($this->filepath, 'r');
|
||||
$skipHeader = true;
|
||||
while ($row = fgetcsv($fileStream)) {
|
||||
if ($skipHeader) {
|
||||
$skipHeader = false;
|
||||
continue;
|
||||
}
|
||||
Word::updateOrCreate(
|
||||
[
|
||||
'name' => $row[$mapping['name']],
|
||||
'translation' => $row[$mapping['translation']],
|
||||
]
|
||||
);
|
||||
}
|
||||
fclose($fileStream);
|
||||
Storage::delete($this->filepath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user