| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace Longyi\Email\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Builder;
- class EmailLog extends Model
- {
- protected $table = 'email_logs';
- // 禁用自动时间戳,因为表中使用的是 created_at 而不是 timestamps
- public $timestamps = true;
- protected $fillable = [
- 'recipient_email',
- 'recipient_name',
- 'subject',
- 'content',
- 'template',
- 'status',
- 'type',
- 'metadata',
- 'sent_at',
- 'error_message',
- ];
- protected $casts = [
- 'metadata' => 'array',
- 'sent_at' => 'datetime',
- ];
- /**
- * Scope: 已发送的邮件
- */
- public function scopeSuccessful(Builder $query): Builder
- {
- return $query->where('status', 'sent');
- }
- /**
- * Scope: 失败的邮件
- */
- public function scopeFailed(Builder $query): Builder
- {
- return $query->where('status', 'failed');
- }
- /**
- * Scope: 待发送的邮件
- */
- public function scopePending(Builder $query): Builder
- {
- return $query->where('status', 'pending');
- }
- /**
- * 获取状态文本
- */
- public function getStatusTextAttribute(): string
- {
- return match($this->status) {
- 'sent' => '已发送',
- 'failed' => '失败',
- 'pending' => '待发送',
- default => '未知',
- };
- }
- /**
- * 获取状态颜色类
- */
- public function getStatusColorClassAttribute(): string
- {
- return match($this->status) {
- 'sent' => 'bg-green-100 text-green-600',
- 'failed' => 'bg-red-100 text-red-600',
- 'pending' => 'bg-yellow-100 text-yellow-600',
- default => 'bg-gray-100 text-gray-600',
- };
- }
- }
|