Skip to content

Commit 8831e7f

Browse files
committed
feat: Add models and migrations for e-commerce functionality
- Created CartItem model and migration for managing cart items. - Created Category model and migration for product categorization. - Created ContentPage model and migration for managing content pages. - Created Order model and migration for handling customer orders. - Created OrderItem model and migration for managing items within orders. - Created Payment model and migration for processing payments. - Created Product model and migration for managing products. - Created ProductVariant model and migration for handling product variants. - Created Shipment model and migration for managing shipments. - Added migrations for creating necessary database tables for categories, products, product variants, attributes, and their values. - Implemented relationships and methods in models to support e-commerce operations.
1 parent 7d741bb commit 8831e7f

31 files changed

+1835
-0
lines changed

backend/app/Models/Address.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
10+
class Address extends Model
11+
{
12+
use HasFactory;
13+
14+
protected $fillable = [
15+
'user_id',
16+
'type',
17+
'first_name',
18+
'last_name',
19+
'company',
20+
'address_line_1',
21+
'address_line_2',
22+
'city',
23+
'state',
24+
'postal_code',
25+
'country',
26+
'phone',
27+
'is_default',
28+
];
29+
30+
protected $casts = [
31+
'is_default' => 'boolean',
32+
];
33+
34+
/**
35+
* Get the user that owns this address
36+
*/
37+
public function user(): BelongsTo
38+
{
39+
return $this->belongsTo(User::class);
40+
}
41+
42+
/**
43+
* Get the orders that use this as billing address
44+
*/
45+
public function billingOrders(): HasMany
46+
{
47+
return $this->hasMany(Order::class, 'billing_address_id');
48+
}
49+
50+
/**
51+
* Get the orders that use this as shipping address
52+
*/
53+
public function shippingOrders(): HasMany
54+
{
55+
return $this->hasMany(Order::class, 'shipping_address_id');
56+
}
57+
58+
/**
59+
* Get the shipments that use this address
60+
*/
61+
public function shipments(): HasMany
62+
{
63+
return $this->hasMany(Shipment::class);
64+
}
65+
66+
/**
67+
* Get the full name for this address
68+
*/
69+
public function getFullNameAttribute(): string
70+
{
71+
return "{$this->first_name} {$this->last_name}";
72+
}
73+
74+
/**
75+
* Get the full address as a formatted string
76+
*/
77+
public function getFormattedAddressAttribute(): string
78+
{
79+
$parts = array_filter([
80+
$this->address_line_1,
81+
$this->address_line_2,
82+
$this->city,
83+
$this->state . ' ' . $this->postal_code,
84+
$this->country,
85+
]);
86+
87+
return implode(', ', $parts);
88+
}
89+
90+
/**
91+
* Set this address as default for the user
92+
*/
93+
public function setAsDefault(): void
94+
{
95+
// Remove default status from other addresses of same type
96+
static::where('user_id', $this->user_id)
97+
->where('type', $this->type)
98+
->where('id', '!=', $this->id)
99+
->update(['is_default' => false]);
100+
101+
$this->update(['is_default' => true]);
102+
}
103+
104+
/**
105+
* Get the default address for a user and type
106+
*/
107+
public static function getDefaultForUser(int $userId, string $type): ?Address
108+
{
109+
return static::where('user_id', $userId)
110+
->where('type', $type)
111+
->where('is_default', true)
112+
->first();
113+
}
114+
}

backend/app/Models/Attribute.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
10+
class Attribute extends Model
11+
{
12+
use HasFactory;
13+
14+
protected $fillable = [
15+
'name',
16+
'type',
17+
'is_required',
18+
'sort_order',
19+
];
20+
21+
protected $casts = [
22+
'is_required' => 'boolean',
23+
];
24+
25+
/**
26+
* Get the attribute values for this attribute
27+
*/
28+
public function attributeValues(): HasMany
29+
{
30+
return $this->hasMany(AttributeValue::class);
31+
}
32+
33+
/**
34+
* Get the products that have this attribute
35+
*/
36+
public function products(): BelongsToMany
37+
{
38+
return $this->belongsToMany(Product::class, 'product_attribute_values')
39+
->withPivot('attribute_value_id');
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
10+
class AttributeValue extends Model
11+
{
12+
use HasFactory;
13+
14+
protected $fillable = [
15+
'attribute_id',
16+
'value',
17+
'sort_order',
18+
];
19+
20+
/**
21+
* Get the attribute that owns this value
22+
*/
23+
public function attribute(): BelongsTo
24+
{
25+
return $this->belongsTo(Attribute::class);
26+
}
27+
28+
/**
29+
* Get the products that have this attribute value
30+
*/
31+
public function products(): BelongsToMany
32+
{
33+
return $this->belongsToMany(Product::class, 'product_attribute_values');
34+
}
35+
}

backend/app/Models/Bundle.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
9+
class Bundle extends Model
10+
{
11+
use HasFactory;
12+
13+
protected $fillable = [
14+
'name',
15+
'slug',
16+
'description',
17+
'price',
18+
'compare_at_price',
19+
'is_active',
20+
];
21+
22+
protected $casts = [
23+
'price' => 'decimal:2',
24+
'compare_at_price' => 'decimal:2',
25+
'is_active' => 'boolean',
26+
];
27+
28+
/**
29+
* Get the bundle items for this bundle
30+
*/
31+
public function bundleItems(): HasMany
32+
{
33+
return $this->hasMany(BundleItem::class);
34+
}
35+
36+
/**
37+
* Get the product variants in this bundle
38+
*/
39+
public function productVariants()
40+
{
41+
return $this->belongsToMany(ProductVariant::class, 'bundle_items')
42+
->withPivot('quantity', 'sort_order');
43+
}
44+
45+
/**
46+
* Get the current price (sale price if available, otherwise regular price)
47+
*/
48+
public function getCurrentPrice(): float
49+
{
50+
return $this->compare_at_price ?? $this->price;
51+
}
52+
53+
/**
54+
* Check if bundle is on sale
55+
*/
56+
public function isOnSale(): bool
57+
{
58+
return $this->compare_at_price !== null && $this->compare_at_price > $this->price;
59+
}
60+
61+
/**
62+
* Calculate the total value of all items in the bundle
63+
*/
64+
public function getTotalValue(): float
65+
{
66+
return $this->bundleItems->sum(function ($item) {
67+
return $item->productVariant->getCurrentPrice() * $item->quantity;
68+
});
69+
}
70+
71+
/**
72+
* Get the savings amount if on sale
73+
*/
74+
public function getSavings(): float
75+
{
76+
if (!$this->isOnSale()) {
77+
return 0;
78+
}
79+
80+
return $this->compare_at_price - $this->price;
81+
}
82+
83+
/**
84+
* Get the savings percentage if on sale
85+
*/
86+
public function getSavingsPercentage(): float
87+
{
88+
if (!$this->isOnSale()) {
89+
return 0;
90+
}
91+
92+
return round(($this->getSavings() / $this->compare_at_price) * 100, 1);
93+
}
94+
}

backend/app/Models/BundleItem.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class BundleItem extends Model
10+
{
11+
use HasFactory;
12+
13+
protected $fillable = [
14+
'bundle_id',
15+
'product_variant_id',
16+
'quantity',
17+
'sort_order',
18+
];
19+
20+
/**
21+
* Get the bundle that owns this item
22+
*/
23+
public function bundle(): BelongsTo
24+
{
25+
return $this->belongsTo(Bundle::class);
26+
}
27+
28+
/**
29+
* Get the product variant for this bundle item
30+
*/
31+
public function productVariant(): BelongsTo
32+
{
33+
return $this->belongsTo(ProductVariant::class);
34+
}
35+
36+
/**
37+
* Get the line total for this bundle item
38+
*/
39+
public function getLineTotal(): float
40+
{
41+
return $this->productVariant->getCurrentPrice() * $this->quantity;
42+
}
43+
}

0 commit comments

Comments
 (0)