feat: refactor product attributes handling to use product_attribute table and update related models and requests

This commit is contained in:
2026-06-30 16:35:56 -03:00
parent 191a40fcb9
commit cb15905c46
15 changed files with 189 additions and 63 deletions

View File

@@ -21,25 +21,13 @@ class StoreProductVariantRequest extends FormRequest
return [
'stock' => ['sometimes', 'integer', 'min:0'],
'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [
'definitions.*.products_attribute_id' => [
'required',
'integer',
'distinct',
Rule::exists('attribute', 'id')->where(
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
Rule::exists('products_attributes', 'id')->where(
fn ($query) => $query->where('product_id', $this->route('producto')?->id)
),
function ($attribute, $value, $fail) {
$productId = $this->route('producto')?->id;
if ($productId) {
$exists = \Illuminate\Support\Facades\DB::table('products_attributes')
->where('product_id', $productId)
->where('attribute_id', $value)
->exists();
if (!$exists) {
$fail('The selected attribute is not associated with the product.');
}
}
},
],
'definitions.*.value' => ['nullable', 'string'],
'images' => ['sometimes', 'nullable', 'array'],

View File

@@ -21,25 +21,13 @@ class UpdateProductVariantRequest extends FormRequest
return [
'stock' => ['sometimes', 'integer', 'min:0'],
'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [
'definitions.*.products_attribute_id' => [
'required',
'integer',
'distinct',
Rule::exists('attribute', 'id')->where(
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
Rule::exists('products_attributes', 'id')->where(
fn ($query) => $query->where('product_id', $this->route('producto')?->id)
),
function ($attribute, $value, $fail) {
$productId = $this->route('producto')?->id;
if ($productId) {
$exists = \Illuminate\Support\Facades\DB::table('products_attributes')
->where('product_id', $productId)
->where('attribute_id', $value)
->exists();
if (!$exists) {
$fail('The selected attribute is not associated with the product.');
}
}
},
],
'definitions.*.value' => ['nullable', 'string'],
'images' => ['sometimes', 'nullable', 'array'],