Files
chatroom/app/Models/UserPurchase.php
T
lkddi e5a35779f8 修复:UserPurchase 模型补充 item() 关联别名
婚姻系统 whereHas('item') / with('item') 需要此方法,
原模型只有 shopItem(),现在加 item() 作为别名指向同一外键。
2026-03-01 15:36:22 +08:00

66 lines
1.4 KiB
PHP

<?php
/**
* 文件功能:用户购买记录模型
* 对应 user_purchases 表,追踪每次商品购买的状态与有效期
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserPurchase extends Model
{
protected $table = 'user_purchases';
protected $fillable = [
'user_id', 'shop_item_id', 'status', 'price_paid',
'expires_at', 'used_at',
];
protected $casts = [
'expires_at' => 'datetime',
'used_at' => 'datetime',
];
/**
* 购买记录所属用户
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* 购买记录对应的商品(shopItem 别名,兼容婚姻系统调用)
*/
public function item(): BelongsTo
{
return $this->belongsTo(ShopItem::class, 'shop_item_id');
}
/**
* 购买记录对应的商品
*/
public function shopItem(): BelongsTo
{
return $this->belongsTo(ShopItem::class, 'shop_item_id');
}
/**
* 判断周卡是否仍在有效期内
*/
public function isAlive(): bool
{
if ($this->status !== 'active') {
return false;
}
if ($this->expires_at && $this->expires_at->isPast()) {
return false;
}
return true;
}
}