feat: refactor product attributes handling to use product_attribute table and update related models and requests
This commit is contained in:
@@ -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'],
|
||||
|
||||
@@ -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'],
|
||||
|
||||
Reference in New Issue
Block a user