diff --git a/beike/Models/Product.php b/beike/Models/Product.php index 37fe4eaa..3f490053 100644 --- a/beike/Models/Product.php +++ b/beike/Models/Product.php @@ -22,6 +22,11 @@ class Product extends Base return $this->belongsToMany(Category::class, ProductCategory::class)->withTimestamps(); } + public function productCategories() + { + return $this->hasMany(ProductCategory::class); + } + public function description() { return $this->hasOne(ProductDescription::class)->where('locale', locale()); @@ -42,9 +47,9 @@ class Product extends Base return $this->hasOne(ProductSku::Class)->where('is_default', 1); } - public function manufacturer() + public function brand() { - return $this->hasOne(Brand::Class, 'id', 'manufacturer_id'); + return $this->belongsTo(Brand::Class, 'brand_id', 'id'); } public function getPriceFormattedAttribute(): string diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 57b73b54..65bfb76c 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use Beike\Models\Product; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder @@ -10,9 +11,34 @@ class DatabaseSeeder extends Seeder * Seed the application's database. * * @return void + * @throws \Throwable */ public function run() { // \App\Models\User::factory(10)->create(); + // $this->duplicate(); + } + + /** + * @throws \Throwable + */ + private function duplicate(): void + { + $baseProduct = Product::with(['productCategories', 'descriptions', 'skus'])->findOrFail(2); + $newProduct = $baseProduct->replicate(); + $newProduct->saveOrFail(); + $newProductId = $newProduct->id; + + if ($newProductId) { + $relations = $baseProduct->getRelations(); + foreach ($relations as $name => $relation) { + dump($name); + foreach ($relation as $relationRecord) { + $newRelationship = $relationRecord->replicate(); + $newRelationship->product_id = $newProductId; + $newRelationship->push(); + } + } + } } }