Files
portal-api/app/Models/Mail.php
T
2026-10-05 09:30:24 +07:00

52 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Mail extends Model
{
use HasFactory;
protected $table = 'mail';
protected $fillable = [
'campaign_id',
'template_id',
'subject',
'to',
'cc',
'customer_id',
'message',
'status'
];
// ponytail: status is stored as tinyint (1=sent, 2=failed, 3=pending) but the
// admin API/UI contract expects string enums; map it here so every reader agrees.
public const STATUS_MAP = [1 => 'sent', 2 => 'failed', 3 => 'pending'];
protected function status(): \Illuminate\Database\Eloquent\Casts\Attribute
{
return \Illuminate\Database\Eloquent\Casts\Attribute::make(
get: fn ($value) => self::STATUS_MAP[$value] ?? self::STATUS_MAP[1],
set: fn ($value) => is_string($value) ? (array_search($value, self::STATUS_MAP, true) ?: 1) : $value,
);
}
public function campaign()
{
return $this->belongsTo(MailCampaign::class, 'campaign_id');
}
public function customer()
{
return $this->belongsTo(Customer::class, 'customer_id');
}
public function template()
{
return $this->belongsTo(Template::class, 'template_id');
}
}