$name, 'sku' => $sku, 'category' => $row['category'] ?? null, 'unit' => $row['unit'] ?? 'pcs', 'unit_cost' => $row['unit_cost'] ?? 0, 'description' => $row['description'] ?? null, ] ); // Handle group assignment $groupName = trim($row['group'] ?? ''); if ($groupName) { $group = MaterialGroup::firstOrCreate( ['name' => $groupName], ['description' => "Auto-created from import"] ); $group->materials()->syncWithoutDetaching([$material->id]); if (!in_array($groupName, $this->groupsCreated)) { $this->groupsCreated[] = $groupName; } } $this->importedCount++; } } public function rules(): array { return [ 'name' => 'required|string|max:255', 'sku' => 'nullable|string|max:50', 'category' => 'nullable|string|max:50', 'unit' => 'nullable|string|max:20', 'unit_cost' => 'nullable|numeric|min:0', 'description' => 'nullable|string', 'group' => 'nullable|string|max:255', ]; } public function getImportedCount(): int { return $this->importedCount; } public function getSkippedCount(): int { return $this->skippedCount; } public function getGroupsCreated(): array { return $this->groupsCreated; } }