61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:用户座驾购买记录模型。
|
||
|
|
*
|
||
|
|
* 对应 user_ride_purchases 表,追踪用户座驾购买、续期、替换和过期状态。
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户座驾购买记录模型
|
||
|
|
* 负责连接用户与座驾,并判断当前记录是否仍有效。
|
||
|
|
*/
|
||
|
|
class UserRidePurchase extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'user_id', 'ride_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);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取购买记录对应座驾。
|
||
|
|
*/
|
||
|
|
public function ride(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Ride::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 判断座驾购买记录是否仍然有效。
|
||
|
|
*/
|
||
|
|
public function isAlive(): bool
|
||
|
|
{
|
||
|
|
if ($this->status !== 'active') {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->expires_at && $this->expires_at->isPast()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|