File: //home/armgroup/libs/app/Imports/AttributeGroupImport.php
<?php
namespace App\Imports;
use App\Models\Product;
use App\Models\AttributeValue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default('none');
class AttributeGroupImport implements
SkipsEmptyRows,
ToCollection,
WithHeadingRow,
WithValidation,
WithChunkReading
{
public function collection(Collection $rows)
{
foreach ($rows as $row) {
try {
$data = $this->mapRow($row);
if (empty($data['excel_name'])) {
continue;
}
$product = Product::where('name', 'like', "%{$data['excel_name']}%")->first();
if (!$product) {
continue;
}
$values = array_filter([
1 => trim($data['val1'] ?? ''),
2 => trim($data['val2'] ?? ''),
3 => trim($data['val3'] ?? ''),
4 => trim($data['val4'] ?? ''),
5 => trim($data['val5'] ?? ''),
// 6 => trim($data['val6'] ?? ''),
// 7 => trim($data['val7'] ?? ''),
// 8 => trim($data['val8'] ?? ''),
], fn ($val) => $val !== '');
$AttributeValue_val1 = optional(
AttributeValue::where('attribute_id',1)
->where('value->fa', $values[1] ?? null)
->first()
)->id ?? null;
$AttributeValue_val2 = optional(
AttributeValue::where('attribute_id',2)
->where('value->fa', $values[2] ?? null)
->first()
)->id ?? null;
$AttributeValue_val3 = optional(
AttributeValue::where('attribute_id',3)
->where('value->fa', $values[3] ?? null)
->first()
)->id ?? null;
$AttributeValue_val4 = optional(
AttributeValue::where('attribute_id',4)
->where('value->fa', $values[4] ?? null)
->first()
)->id ?? null;
$AttributeValue_val5 = optional(
AttributeValue::where('attribute_id',5)
->where('value->fa', $values[5] ?? null)
->first()
)->id ?? null;
// $AttributeValue_val6 = optional(
// AttributeValue::where('attribute_id',6)
// ->where('value->fa', $values[6] ?? null)
// ->first()
// )->id ?? null;
// $AttributeValue_val7 = optional(
// AttributeValue::where('attribute_id',7)
// ->where('value->fa', $values[7] ?? null)
// ->first()
// )->id ?? null;
// $AttributeValue_val8 = optional(
// AttributeValue::where('attribute_id',8)
// ->where('value->fa', $values[8] ?? null)
// ->first()
// )->id ?? null;
$selectedAttributes = [
1 => $AttributeValue_val1,
2 => $AttributeValue_val2,
3 => $AttributeValue_val3,
4 => $AttributeValue_val4,
5 => $AttributeValue_val5,
// 6 => $AttributeValue_val6,
// 7 => $AttributeValue_val7,
// 8 => $AttributeValue_val8,
];
$attributes_data = [];
foreach ($selectedAttributes as $attribute => $value) {
if ($value) {
$attributes_data[] = [
'product_id' => $product->id,
'attribute_id' => $attribute,
'value_id' => $value,
];
}
}
// dd($attributes_data);
if (count($attributes_data)) {
DB::table('attribute_product')->insert($attributes_data);
}
} catch (\Throwable $e) {
Log::error('attribute Import Error', [
'message' => $e->getMessage(),
'row' => $row->toArray(),
]);
}
}
}
private function mapRow($row): array
{
return [
'excel_name' => $this->clean($row['نام'] ?? ''),
'val1' => $this->clean($row['سایر'] ?? ''),
'val2' => $this->clean($row['رنگ'] ?? ''),
'val3' => $this->clean($row['برند'] ?? ''),
'val4' => $this->clean($row['فرم صفحه'] ?? ''),
'val5' => $this->clean($row['جنس'] ?? ''),
];
}
private function clean($value)
{
return trim(str_replace("\u{00A0}", ' ', $value));
}
public function rules(): array
{
return [
'نام' => ['required'],
'سایر' => ['nullable'],
'رنگ' => ['nullable'],
'برند' => ['nullable'],
'فرم صفحه' => ['nullable'],
'جنس' => ['nullable'],
];
}
public function chunkSize(): int
{
return 2000;
}
}