Skip to content

Commit 7788331

Browse files
committed
Refactor wholesale prices relationships in Product, ProductVariation, and VariationAttribute models to use morphMany for polymorphic associations
This commit updates the wholesalePrices method in the Product, ProductVariation, and VariationAttribute models to utilize morphMany, allowing for a more flexible relationship with the WholeSalePrice model. Additionally, the WholeSalePrice model is modified to implement a morphTo relationship, enhancing the polymorphic functionality. The obsolete migration for the whole_sale_prices table has been removed to reflect these changes.
1 parent 7f90cb1 commit 7788331

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

app/Models/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public function getVariations()
123123
});
124124
}
125125

126-
public function wholesalePrices(): HasMany
126+
public function wholesalePrices()
127127
{
128-
return $this->hasMany(WholeSalePrice::class);
128+
return $this->morphMany(WholeSalePrice::class, 'priceable');
129129
}
130130
}

app/Models/ProductVariation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Product;
66
use App\Models\AttributeValue;
77
use App\Models\VariationImage;
8+
use App\Models\WholeSalePrice;
89
use App\Models\VariationAttribute;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -39,5 +40,10 @@ public function images(): HasMany
3940
{
4041
return $this->hasMany(VariationImage::class);
4142
}
43+
44+
public function wholesalePrices()
45+
{
46+
return $this->morphMany(WholeSalePrice::class, 'priceable');
47+
}
4248

4349
}

app/Models/VariationAttribute.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models;
44

55
use App\Models\AttributeValue;
6+
use App\Models\WholeSalePrice;
67
use App\Models\ProductVariation;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -33,4 +34,9 @@ public function attributeValue(): BelongsTo
3334
{
3435
return $this->belongsTo(AttributeValue::class);
3536
}
37+
38+
public function wholesalePrices()
39+
{
40+
return $this->morphMany(WholeSalePrice::class, 'priceable');
41+
}
3642
}

app/Models/WholeSalePrice.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
namespace App\Models;
44

5-
use App\Models\Product;
5+
use App\Models\WholeSalePrice;
66
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
87
use Illuminate\Database\Eloquent\Factories\HasFactory;
98

109
class WholeSalePrice extends Model
1110
{
1211
use HasFactory;
1312

14-
public function product(): BelongsTo
13+
protected $fillable = [
14+
'min_quantity',
15+
'wholesale_price',
16+
'priceable_id',
17+
'priceable_type',
18+
];
19+
20+
public function priceable()
1521
{
16-
return $this->belongsTo(Product::class);
22+
return $this->morphTo();
1723
}
1824
}

database/migrations/2025_08_22_131357_create_whole_sale_prices_table.php renamed to database/migrations/2025_09_05_182104_create_whole_sale_prices_table.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
use App\Models\Product;
4-
use Illuminate\Support\Facades\Schema;
5-
use Illuminate\Database\Schema\Blueprint;
63
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
76

87
return new class extends Migration
98
{
@@ -14,13 +13,16 @@ public function up(): void
1413
{
1514
Schema::create('whole_sale_prices', function (Blueprint $table) {
1615
$table->id();
17-
$table->foreignIdFor(Product::class)
18-
->constrained()
19-
->restrictOnDelete()
20-
->restrictOnUpdate();
16+
17+
$table->string('priceable_id');
18+
$table->string('priceable_type');
19+
2120
$table->string('min_quantity');
2221
$table->string("wholesale_price");
22+
2323
$table->timestamps();
24+
25+
$table->index(['priceable_id', 'priceable_type']);
2426
});
2527
}
2628

0 commit comments

Comments
 (0)