Skip to content

Commit 18ff1f2

Browse files
committed
refactor: rewrite duplicate methods
1 parent 3a0cb98 commit 18ff1f2

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

src/Filament/Resources/ProductResource/Pages/CreateProduct.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,6 @@ protected function mutateFormDataBeforeCreate(array $data): array
3838
return $data;
3939
}
4040

41-
protected function afterCreate(): void
42-
{
43-
if ($this->record) {
44-
$state = $this->form->getRawState();
45-
$propertyData = [];
46-
foreach ($state as $key => $value) {
47-
if (is_string($key) && str_starts_with($key, 'property_values_')) {
48-
$propertyId = str_replace('property_values_', '', $key);
49-
$propertyData[$propertyId] = $value;
50-
}
51-
}
52-
53-
foreach ($propertyData as $propertyId => $values) {
54-
if ($values) {
55-
$valuesToAttach = is_array($values) ? $values : [$values];
56-
$valuesToAttach = array_filter($valuesToAttach);
57-
58-
if (! empty($valuesToAttach)) {
59-
$this->record->propertyValues()->attach($valuesToAttach);
60-
}
61-
}
62-
}
63-
}
64-
}
65-
6641
protected function getFormTenantFlags(): array
6742
{
6843
return ['is_active', 'has_free_delivery'];
@@ -91,9 +66,35 @@ protected function afterCreate(): void
9166
/** @var Product $product */
9267
$product = $this->record;
9368

69+
if (! $product) {
70+
return;
71+
}
72+
9473
$state = $this->form->getState();
74+
$rawState = $this->form->getRawState();
9575
$tenantData = $state['tenant_data'] ?? [];
9676

77+
// Handle property values
78+
$propertyData = [];
79+
foreach ($rawState as $key => $value) {
80+
if (is_string($key) && str_starts_with($key, 'property_values_')) {
81+
$propertyId = str_replace('property_values_', '', $key);
82+
$propertyData[$propertyId] = $value;
83+
}
84+
}
85+
86+
foreach ($propertyData as $propertyId => $values) {
87+
if ($values) {
88+
$valuesToAttach = is_array($values) ? $values : [$values];
89+
$valuesToAttach = array_filter($valuesToAttach);
90+
91+
if (! empty($valuesToAttach)) {
92+
$product->propertyValues()->attach($valuesToAttach);
93+
}
94+
}
95+
}
96+
97+
// Handle tenant/group associations
9798
$isTenancyEnabled = (bool) config('eclipse-catalogue.tenancy.model');
9899
if ($isTenancyEnabled) {
99100
foreach ($tenantData as $tenantId => $data) {

0 commit comments

Comments
 (0)