Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
gilour
/
back
/
app
/
Imports
:
ArticleImport.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?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]), ]); } }