ldc-nature-eleveurs/app/Models/GeneralConfig.php
2024-02-01 10:01:28 +01:00

97 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\GeneralConfigType;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
/**
* @property string $data Data.
* @property \App\Enums\GeneralConfigType $type Type.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class GeneralConfig extends Model
{
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'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => GeneralConfigType::class,
'data' => 'array',
];
/**
* 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);
}
}