Mahdee Rajon
File "ArticleImport.php"
Full Path: /var/www/html/back/app/Imports/ArticleImport.php
File size: 1.33 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Imports;
use App\Domain\Article\Enums\ArticleTypeEnum;
use App\Models\Article;
use App\Models\ArticleGroup;
use App\Models\ArticleToGroup;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
class ArticleImport implements ToModel, WithStartRow
{
protected $currentType = null;
protected $currentGroupId = null;
public function __construct(?string $type = null)
{
$this->currentType = $type;
}
public function startRow(): int
{
return 3;
}
public function model(array $row)
{
if (empty(trim((string)$row[0]))) {
return null;
}
if (isset($row[1])) {
if (!empty(trim((string)$row[1]))) {
$this->currentGroupId = ArticleGroup::query()->firstOrCreate([
'model_id' => 2,
'article_type' => $this->currentType,
'name' => trim((string)$row[1])
]);
}
} else {
$this->currentGroupId = null;
}
return new Article([
'article_group_id' => $this->currentGroupId->id ?? null,
'model_id' => 2,
'article_type' => $this->currentType,
'name' => trim((string)$row[0]),
]);
}
}