Files
portal-api/app/Models/OrderItem.php
T
pbao9 057353a2e1 feat: Enhance product support in Cart and OrderItem models
- Added new fields 'billing_cycle' and 'product_pricing_id' to Cart and OrderItem models.
- Updated product retrieval logic to support both new and legacy systems.
- Introduced relationships for Product and ProductPricings in Cart and OrderItem models.
- Created migration to add new fields to the carts and order_item tables.
- Updated frontend service index to handle new pricing structure and billing cycles.
- Improved UI components for better user experience in product selection and pricing display.
- Refactored action buttons in various views for consistency and improved styling.
2026-02-09 08:04:56 +07:00

68 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class OrderItem extends Model
{
use HasFactory;
protected $table = 'order__order_item';
protected $guarded = [];
public $timestamps = false;
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
public function addons()
{
return $this->hasMany(OrderAddon::class, 'order_item_id');
}
public function getProductAttribute()
{
// New flow: Check if product exists in Product table first
if ($this->product_id) {
$product = Product::find($this->product_id);
if ($product) {
return $product;
}
}
// Legacy flow: fallback to old system
return match ($this->product_type) {
3 => Hosting::find($this->product_id),
4 => Vps::find($this->product_id),
default => null,
};
}
/**
* Relationship: OrderItem belongs to Product (new system)
*/
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: OrderItem has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
public function os()
{
return $this->belongsTo(Os::class, 'os_id');
}
}