EmailLog.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Longyi\Email\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. class EmailLog extends Model
  6. {
  7. protected $table = 'email_logs';
  8. // 禁用自动时间戳,因为表中使用的是 created_at 而不是 timestamps
  9. public $timestamps = true;
  10. protected $fillable = [
  11. 'recipient_email',
  12. 'recipient_name',
  13. 'subject',
  14. 'content',
  15. 'template',
  16. 'status',
  17. 'type',
  18. 'metadata',
  19. 'sent_at',
  20. 'error_message',
  21. ];
  22. protected $casts = [
  23. 'metadata' => 'array',
  24. 'sent_at' => 'datetime',
  25. ];
  26. /**
  27. * Scope: 已发送的邮件
  28. */
  29. public function scopeSuccessful(Builder $query): Builder
  30. {
  31. return $query->where('status', 'sent');
  32. }
  33. /**
  34. * Scope: 失败的邮件
  35. */
  36. public function scopeFailed(Builder $query): Builder
  37. {
  38. return $query->where('status', 'failed');
  39. }
  40. /**
  41. * Scope: 待发送的邮件
  42. */
  43. public function scopePending(Builder $query): Builder
  44. {
  45. return $query->where('status', 'pending');
  46. }
  47. /**
  48. * 获取状态文本
  49. */
  50. public function getStatusTextAttribute(): string
  51. {
  52. return match($this->status) {
  53. 'sent' => '已发送',
  54. 'failed' => '失败',
  55. 'pending' => '待发送',
  56. default => '未知',
  57. };
  58. }
  59. /**
  60. * 获取状态颜色类
  61. */
  62. public function getStatusColorClassAttribute(): string
  63. {
  64. return match($this->status) {
  65. 'sent' => 'bg-green-100 text-green-600',
  66. 'failed' => 'bg-red-100 text-red-600',
  67. 'pending' => 'bg-yellow-100 text-yellow-600',
  68. default => 'bg-gray-100 text-gray-600',
  69. };
  70. }
  71. }