ldc-crm/app/Models/ClaimMailTemplate.php

100 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-08-04 14:50:03 +02:00
use Illuminate\Support\Facades\Cache;
/**
* @property integer $id Mail template id.
* @property string $label Mail template label.
* @property string $content Mail template content.
* @property \App\Models\Brand $brand_id Mail template associated Brand.
* @property-read \Illuminate\Support\Carbon $created_at Mail template created date.
* @property-read \Illuminate\Support\Carbon $updated_at Mail template updated date.
*/
class ClaimMailTemplate extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'label',
'content',
'brand_id',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::created(function () {
self::resetCache();
});
static::updated(function () {
self::resetCache();
});
static::deleted(function () {
self::resetCache();
});
}
2025-08-04 14:50:03 +02:00
// * METHODS
/**
* Get all templates associated to the band.
*
* @param integer $brandModelKey
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getAllModelsFromBrand(int $brandModelKey): \Illuminate\Database\Eloquent\Collection
{
return self::getTemplates()->filter(
fn($model) => $model->brand->getKey() === $brandModelKey
)->values();
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('templates');
self::getTemplates();
}
/**
* Get all templates.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getTemplates(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('templates', 86400, function () {
return self::query()->orderBy('label', 'ASC')->get();
});
}
// * RELATIONS
/**
* Brand belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Brand::class);
}
}