ldc-nature-eleveurs/app/Models/GeneralConfig.php
2024-02-05 09:55:20 +01:00

93 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\GeneralConfigType;
use App\Traits\Models\ActivityLog;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class GeneralConfig extends Model
{
use ActivityLog;
use MiniCrudModel;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'type';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'type',
'data',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => GeneralConfigType::class,
'data' => 'object',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::created(function () {
Cache::delete('GeneralConfigGlobal');
});
static::updated(function () {
Cache::delete('GeneralConfigGlobal');
});
static::deleted(function () {
Cache::delete('GeneralConfigGlobal');
});
}
/**
* Get from cached all configs
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getCachedConfigs(): \Illuminate\Database\Eloquent\Collection
{
if (!Cache::has('GeneralConfigGlobal')) {
self::setCachedConfigs();
}
return Cache::get('GeneralConfigGlobal');
}
/**
* Put in cache all configs
*
* @return void
*/
protected static function setCachedConfigs()
{
$configs = GeneralConfig::all();
Cache::put('GeneralConfigGlobal', $configs);
}
}