From a294bb2d284c08f7a023c6481457feb62172dc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Fri, 10 May 2024 12:16:34 +0200 Subject: [PATCH 1/2] Add attributes to ProductOption --- ...dd_attributes_to_product_options_table.php | 22 +++++++++++++++++++ packages/core/src/Base/AttributeManifest.php | 2 ++ packages/core/src/Models/ProductOption.php | 20 +++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 packages/core/database/migrations/2024_05_10_100000_add_attributes_to_product_options_table.php diff --git a/packages/core/database/migrations/2024_05_10_100000_add_attributes_to_product_options_table.php b/packages/core/database/migrations/2024_05_10_100000_add_attributes_to_product_options_table.php new file mode 100644 index 0000000000..3358d5fdaa --- /dev/null +++ b/packages/core/database/migrations/2024_05_10_100000_add_attributes_to_product_options_table.php @@ -0,0 +1,22 @@ +prefix.'product_options', function (Blueprint $table) { + $table->json('attribute_data')->nullable(); + }); + } + + public function down() + { + Schema::table($this->prefix.'product_options', function ($table) { + $table->dropColumn('attribute_data'); + }); + } +} diff --git a/packages/core/src/Base/AttributeManifest.php b/packages/core/src/Base/AttributeManifest.php index c52d0faada..57443ab3a1 100644 --- a/packages/core/src/Base/AttributeManifest.php +++ b/packages/core/src/Base/AttributeManifest.php @@ -9,6 +9,7 @@ use Lunar\Models\Collection as ModelsCollection; use Lunar\Models\Customer; use Lunar\Models\Product; +use Lunar\Models\ProductOption; use Lunar\Models\ProductVariant; class AttributeManifest @@ -23,6 +24,7 @@ class AttributeManifest protected $baseTypes = [ Product::class, ProductVariant::class, + ProductOption::class, ModelsCollection::class, Customer::class, Brand::class, diff --git a/packages/core/src/Models/ProductOption.php b/packages/core/src/Models/ProductOption.php index 581c8a54b7..b2e4fc4986 100644 --- a/packages/core/src/Models/ProductOption.php +++ b/packages/core/src/Models/ProductOption.php @@ -7,7 +7,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Lunar\Base\BaseModel; +use Lunar\Base\Casts\AsAttributeData; +use Lunar\Base\Traits\HasAttributes; use Lunar\Base\Traits\HasMacros; use Lunar\Base\Traits\HasMedia; use Lunar\Base\Traits\HasTranslations; @@ -21,11 +24,13 @@ * @property \Illuminate\Support\Collection $label * @property int $position * @property ?string $handle + * @property ?array $attribute_data * @property ?\Illuminate\Support\Carbon $created_at * @property ?\Illuminate\Support\Carbon $updated_at */ class ProductOption extends BaseModel implements SpatieHasMedia { + use HasAttributes; use HasFactory; use HasMacros; use HasMedia; @@ -41,6 +46,7 @@ class ProductOption extends BaseModel implements SpatieHasMedia 'name' => AsCollection::class, 'label' => AsCollection::class, 'shared' => 'boolean', + 'attribute_data' => AsAttributeData::class, ]; /** @@ -88,4 +94,18 @@ public function products(): BelongsToMany "{$prefix}product_product_option" )->withPivot(['position'])->orderByPivot('position'); } + + /** + * Get the mapped attributes relation. + */ + public function mappedAttributes(): MorphToMany + { + $prefix = config('lunar.database.table_prefix'); + + return $this->morphToMany( + Attribute::class, + 'attributable', + "{$prefix}attributables" + )->withTimestamps(); + } } From b62155ebf61d08dc41e69e631a083841b0b36499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Fri, 10 May 2024 12:17:00 +0200 Subject: [PATCH 2/2] Add attributes to ProductOptionValue --- ...ributes_to_product_option_values_table.php | 22 +++++++++++++++++++ packages/core/src/Base/AttributeManifest.php | 2 ++ .../core/src/Models/ProductOptionValue.php | 20 +++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 packages/core/database/migrations/2024_05_10_100010_add_attributes_to_product_option_values_table.php diff --git a/packages/core/database/migrations/2024_05_10_100010_add_attributes_to_product_option_values_table.php b/packages/core/database/migrations/2024_05_10_100010_add_attributes_to_product_option_values_table.php new file mode 100644 index 0000000000..4f3fd5f2d9 --- /dev/null +++ b/packages/core/database/migrations/2024_05_10_100010_add_attributes_to_product_option_values_table.php @@ -0,0 +1,22 @@ +prefix.'product_option_values', function (Blueprint $table) { + $table->json('attribute_data')->nullable(); + }); + } + + public function down() + { + Schema::table($this->prefix.'product_option_values', function ($table) { + $table->dropColumn('attribute_data'); + }); + } +} diff --git a/packages/core/src/Base/AttributeManifest.php b/packages/core/src/Base/AttributeManifest.php index 57443ab3a1..0b38965c95 100644 --- a/packages/core/src/Base/AttributeManifest.php +++ b/packages/core/src/Base/AttributeManifest.php @@ -10,6 +10,7 @@ use Lunar\Models\Customer; use Lunar\Models\Product; use Lunar\Models\ProductOption; +use Lunar\Models\ProductOptionValue; use Lunar\Models\ProductVariant; class AttributeManifest @@ -25,6 +26,7 @@ class AttributeManifest Product::class, ProductVariant::class, ProductOption::class, + ProductOptionValue::class, ModelsCollection::class, Customer::class, Brand::class, diff --git a/packages/core/src/Models/ProductOptionValue.php b/packages/core/src/Models/ProductOptionValue.php index 6b0999b34f..346f705cd6 100644 --- a/packages/core/src/Models/ProductOptionValue.php +++ b/packages/core/src/Models/ProductOptionValue.php @@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Lunar\Base\BaseModel; +use Lunar\Base\Casts\AsAttributeData; +use Lunar\Base\Traits\HasAttributes; use Lunar\Base\Traits\HasMacros; use Lunar\Base\Traits\HasMedia; use Lunar\Base\Traits\HasTranslations; @@ -18,11 +21,13 @@ * @property int $product_option_id * @property string $name * @property int $position + * @property ?array $attribute_data * @property ?\Illuminate\Support\Carbon $created_at * @property ?\Illuminate\Support\Carbon $updated_at */ class ProductOptionValue extends BaseModel implements SpatieHasMedia { + use HasAttributes; use HasFactory; use HasMacros; use HasMedia; @@ -35,6 +40,7 @@ class ProductOptionValue extends BaseModel implements SpatieHasMedia */ protected $casts = [ 'name' => AsCollection::class, + 'attribute_data' => AsAttributeData::class, ]; /** @@ -69,4 +75,18 @@ public function variants(): BelongsToMany 'variant_id', ); } + + /** + * Get the mapped attributes relation. + */ + public function mappedAttributes(): MorphToMany + { + $prefix = config('lunar.database.table_prefix'); + + return $this->morphToMany( + Attribute::class, + 'attributable', + "{$prefix}attributables" + )->withTimestamps(); + } }