Files
chatroom/app/Models/UserCurrencyLog.php

102 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 文件功能:用户积分流水 Eloquent 模型
* 对应表user_currency_logs
* 只读写,不允许 update流水记录不可更改
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserCurrencyLog extends Model
{
/**
* 只有 created_at没有 updated_at流水记录只写不改
*/
public const UPDATED_AT = null;
/**
* 允许批量赋值的字段
*/
protected $fillable = [
'user_id',
'username',
'currency',
'amount',
'balance_after',
'source',
'remark',
'room_id',
];
/**
* 字段类型转换
*/
protected $casts = [
'amount' => 'integer',
'balance_after' => 'integer',
'room_id' => 'integer',
'created_at' => 'datetime',
];
// ─── 关联 ─────────────────────────────────────────────────
/**
* 关联用户(流水属于哪个用户)
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
// ─── 作用域Scope快捷查询 ──────────────────────────────
/**
* 按货币类型过滤
*/
public function scopeCurrency(Builder $query, string $currency): Builder
{
return $query->where('currency', $currency);
}
/**
* 今日数据
*/
public function scopeToday(Builder $query): Builder
{
return $query->whereDate('created_at', today());
}
/**
* 指定日期数据
*/
public function scopeOnDate(Builder $query, string $date): Builder
{
return $query->whereDate('created_at', $date);
}
/**
* 仅正向增加(用于排行榜,不算消耗)
*/
public function scopeGain(Builder $query): Builder
{
return $query->where('amount', '>', 0);
}
/**
* 按来源过滤
*/
public function scopeSource(Builder $query, string $source): Builder
{
return $query->where('source', $source);
}
}