File: //home/armgroup/libs/app/Imports/ProductsImport.php
<?php
namespace App\Imports;
use App\Models\Product;
use App\Models\Category;
use App\Models\Language;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default('none');
class ProductsImport implements SkipsEmptyRows, ToCollection, WithHeadingRow, WithValidation
{
public function collection(Collection $rows)
{
foreach ($rows as $row) {
try {
$data = $this->mapRow($row);
$name = $data['name'];
$description = html_entity_decode($data['description']);
$baseSlug = slug($data['slug'] ?? $name);
// $count = Product::where('slug', 'LIKE', $baseSlug . '%')->count();
// $slug = $count ? $baseSlug . '-' . $count : $baseSlug;
$baseSlug = slug($data['slug'] ?? $name);
if (Product::where('slug', $baseSlug)->exists()) {
\Log::info('Duplicate product skipped', [
'name' => $name,
'slug' => $baseSlug
]);
continue;
}
$slug = $baseSlug;
$product = Product::create([
'name' => $data['name'],
'slug' => $slug,
'latin_name' => $data['latin_name'],
'brand_id' => $data['brand_id'],
'weight' => $data['weight'],
'meta_title' => $data['meta_title'],
'short_description' => $data['short_description'],
'description' => html_entity_decode($data['description']),
'price' => $data['price'] ?? 0,
'stock' => $data['stock'] ?? 0,
'code' => $data['code'] ?? 0,
'country_code' => $data['country_code'] ?? 0,
'status' => $data['status'] ?? 1,
'image' => 'storage/photos/shares/'.$data['image'] ?? null,
]);
$categories = [];
if (!empty($data['category_id'])) {
$categoryIds = is_string($data['category_id'])
? explode(',', $data['category_id'])
: [$data['category_id']];
foreach ($categoryIds as $index => $categoryId) {
$categoryId = (int) trim($categoryId);
if ($categoryId && Category::find($categoryId)) {
$categories[$categoryId] = [
'is_main' => $index === 0
];
}
}
}
if (!empty($categories)) {
$product->categories()->attach($categories);
}
} catch (\Exception $e) {
Log::error('Product Import Error', [
'message' => $e->getMessage(),
'row' => $row
]);
continue;
}
}
}
private function mapRow($row)
{
return [
'excel_id' => $this->getValue($row, ['شناسه محصول', 'id', 'product_id']),
'name' => $this->getValue($row, ['نام محصول', 'name']),
'latin_name' => $this->getValue($row, ['نام انگلیسی', 'latin_name']),
'brand_id' => $this->getValue($row, ['برند', 'brand_id']),
'slug' => $this->getValue($row, ['اسلاگ', 'slug']),
'weight' => $this->getValue($row, ['وزن', 'weight']),
'meta_title' => $this->getValue($row, ['عنوان صفحه', 'meta_title']),
'short_description' => $this->getValue($row, ['توضیحات کوتاه', 'short_description']),
'image' => $this->getValue($row, ['عکس', 'image']),
'price' => $this->getValue($row, ['قیمت', 'price']),
'stock' => $this->getValue($row, ['موجودی', 'stock']),
'code' => $this->getValue($row, ['کد', 'code']),
'country_code' => $this->getValue($row, ['کد کشور', 'country_code']),
'category_id' => $this->getValue($row, ['دسته بندی', 'category_id']),
'status' => $this->getValue($row, ['وضعیت', 'status']),
'description' => $this->getValue($row, ['توضیحات', 'description']),
];
}
private function getValue($row, array $keys)
{
foreach ($keys as $key) {
if (isset($row[$key]) && $row[$key] !== null) {
return $row[$key];
}
}
return null;
}
public function rules(): array
{
return [
'نام محصول' => ['required', 'max:255'],
'قیمت' => ['nullable', 'numeric'],
'موجودی' => ['nullable', 'integer'],
];
}
public function batchSize(): int
{
return 1000;
}
}