diff --git a/packages/core/database/migrations/2024_01_04_100013_add_attributes_to_customer_groups_table.php b/packages/core/database/migrations/2024_01_04_100013_add_attributes_to_customer_groups_table.php new file mode 100644 index 0000000000..196aff0e69 --- /dev/null +++ b/packages/core/database/migrations/2024_01_04_100013_add_attributes_to_customer_groups_table.php @@ -0,0 +1,22 @@ +prefix.'customer_groups', function (Blueprint $table) { + $table->json('attribute_data')->after('default')->nullable(); + }); + } + + public function down() + { + Schema::table($this->prefix.'customer_groups', function ($table) { + $table->dropColumn('attribute_data'); + }); + } +} diff --git a/packages/core/src/Base/AttributeManifest.php b/packages/core/src/Base/AttributeManifest.php index c52d0faada..d54e9d31f2 100644 --- a/packages/core/src/Base/AttributeManifest.php +++ b/packages/core/src/Base/AttributeManifest.php @@ -8,6 +8,7 @@ use Lunar\Models\Brand; use Lunar\Models\Collection as ModelsCollection; use Lunar\Models\Customer; +use Lunar\Models\CustomerGroup; use Lunar\Models\Product; use Lunar\Models\ProductVariant; @@ -25,6 +26,7 @@ class AttributeManifest ProductVariant::class, ModelsCollection::class, Customer::class, + CustomerGroup::class, Brand::class, // Order::class, ]; diff --git a/packages/core/src/Models/CustomerGroup.php b/packages/core/src/Models/CustomerGroup.php index b785109082..c51cb6eebe 100644 --- a/packages/core/src/Models/CustomerGroup.php +++ b/packages/core/src/Models/CustomerGroup.php @@ -5,8 +5,11 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Lunar\Base\BaseModel; +use Lunar\Base\Casts\AsAttributeData; +use Lunar\Base\Traits\HasAttributes; use Lunar\Base\Traits\HasDefaultRecord; use Lunar\Base\Traits\HasMacros; +use Lunar\Base\Traits\HasTranslations; use Lunar\Database\Factories\CustomerGroupFactory; /** @@ -14,6 +17,7 @@ * @property string $name * @property string $handle * @property bool $default + * @property ?array $attribute_data * @property ?\Illuminate\Support\Carbon $created_at * @property ?\Illuminate\Support\Carbon $updated_at */ @@ -22,11 +26,20 @@ class CustomerGroup extends BaseModel use HasDefaultRecord; use HasFactory; use HasMacros; + use HasAttributes; + use HasTranslations; /** * {@inheritDoc} */ protected $guarded = []; + + /** + * {@inheritDoc} + */ + protected $casts = [ + 'attribute_data' => AsAttributeData::class, + ]; /** * Return a new factory instance for the model. @@ -48,4 +61,20 @@ public function customers(): BelongsToMany "{$prefix}customer_customer_group" )->withTimestamps(); } + + /** + * Get the mapped attributes relation. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + public function mappedAttributes() + { + $prefix = config('lunar.database.table_prefix'); + + return $this->morphToMany( + Attribute::class, + 'attributable', + "{$prefix}attributables" + )->withTimestamps(); + } }