feat: implement product attribute options with database migration, resource, and validation requests

This commit is contained in:
ncoronel 2026-06-29 09:48:56 -03:00
parent b0508d79b3
commit 6efda4727f
7 changed files with 10 additions and 1 deletions

View File

@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable([
'attribute_id',
'value',
'label',
'sort_order',
'metadata',

View File

@ -43,6 +43,7 @@ class StoreProductAttributeRequest extends FormRequest
], true)),
'array',
],
'options.*.value' => ['required', 'string', 'max:255'],
'options.*.label' => ['required', 'string', 'max:255'],
'options.*.sort_order' => ['sometimes', 'integer'],
'options.*.metadata' => ['nullable', 'array'],

View File

@ -37,6 +37,7 @@ class UpdateProductAttributeRequest extends FormRequest
'metadata_schema' => ['nullable', 'array'],
'type' => ['required', Rule::enum(FieldType::class)],
'options' => ['sometimes', 'array'],
'options.*.value' => ['required', 'string', 'max:255'],
'options.*.label' => ['required', 'string', 'max:255'],
'options.*.sort_order' => ['sometimes', 'integer'],
'options.*.metadata' => ['nullable', 'array'],

View File

@ -17,6 +17,7 @@ class ProductAttributeOptionResource extends JsonResource
{
return [
'id' => $this->id,
'value' => $this->value,
'label' => $this->label,
'sort_order' => $this->sort_order,
'metadata' => $this->metadata,

View File

@ -14,6 +14,7 @@ return new class extends Migration
Schema::create('attribute_options', function (Blueprint $table) {
$table->id();
$table->foreignId('attribute_id')->constrained('productos_attributes')->cascadeOnUpdate()->cascadeOnDelete();
$table->string('value');
$table->string('label');
$table->unsignedInteger('sort_order')->default(0);
$table->json('metadata')->nullable();

View File

@ -37,7 +37,7 @@ class CartControllerTest extends TestCase
'tenant_codigo' => 'acme',
'codigo' => 'color',
'nombre' => 'Color',
'type' => 'text',
'type' => 'string',
]);
ProductVariantDefinition::query()->create([

View File

@ -24,11 +24,13 @@ class ProductAttributeControllerTest extends TestCase
],
'options' => [
[
'value' => 'red',
'label' => 'Red',
'sort_order' => 1,
'metadata' => ['hex' => '#ff0000'],
],
[
'value' => 'blue',
'label' => 'Blue',
'sort_order' => 2,
'metadata' => ['hex' => '#0000ff'],
@ -41,6 +43,7 @@ class ProductAttributeControllerTest extends TestCase
->assertJsonPath('data.tenant_codigo', 'acme')
->assertJsonPath('data.codigo', 'color')
->assertJsonPath('data.type', 'select')
->assertJsonPath('data.options.0.value', 'red')
->assertJsonPath('data.options.0.label', 'Red')
->assertJsonPath('data.options.1.metadata.hex', '#0000ff');
@ -51,6 +54,7 @@ class ProductAttributeControllerTest extends TestCase
]);
$this->assertDatabaseHas('attribute_options', [
'value' => 'red',
'label' => 'Red',
'sort_order' => 1,
]);