refactor(catalog): Complete catalog refactor to simplify its data model and its querying.
source commits: refactor/catalog
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace App\Domains\Cart\Models;
|
||||
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -10,8 +11,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'cart_id',
|
||||
'buyable_id',
|
||||
'buyable_type',
|
||||
'catalog_item_id',
|
||||
'variant_id',
|
||||
'cantidad',
|
||||
])]
|
||||
class CartItem extends Model
|
||||
@@ -24,8 +25,8 @@ class CartItem extends Model
|
||||
{
|
||||
return [
|
||||
'cart_id' => 'integer',
|
||||
'buyable_id' => 'integer',
|
||||
'buyable_type' => 'string',
|
||||
'catalog_item_id' => 'integer',
|
||||
'variant_id' => 'integer',
|
||||
'cantidad' => 'integer',
|
||||
];
|
||||
}
|
||||
@@ -38,11 +39,20 @@ class CartItem extends Model
|
||||
return $this->belongsTo(Cart::class, 'cart_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
*/
|
||||
public function buyable()
|
||||
/** @return BelongsTo<CatalogItem, $this> */
|
||||
public function catalogItem(): BelongsTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
return $this->belongsTo(CatalogItem::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Variant, $this> */
|
||||
public function variant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Variant::class);
|
||||
}
|
||||
|
||||
public function selectedItem(): CatalogItem|Variant|null
|
||||
{
|
||||
return $this->variant ?? $this->catalogItem;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user