ldc-legaulois/app/Models/Actu.php
2021-09-16 19:31:57 +02:00

71 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Actu extends Model
{
protected $fillable = [
'title',
'header',
'content',
'status'
];
protected static function booted()
{
static::creating(function (Actu $actu) {
$actu->header = clean($actu->header);
$actu->content = clean($actu->content);
});
static::updating(function (Actu $actu) {
$actu->header = clean($actu->header);
$actu->content = clean($actu->content);
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::created(function (Actu $actu) {
Cache::delete('ActusGlobal');
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::updated(function (Actu $actu) {
Cache::delete('ActusGlobal');
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::deleted(function (Actu $actu) {
Cache::delete('ActusGlobal');
});
}
public function getRouteKeyName()
{
return 'slug';
}
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function notifiable()
{
return $this->morphOne(Notifications::class, 'notifiable');
}
public static function getCachedActus()
{
if (!Cache::has('ActusGlobal')) {
self::setCachedActus();
}
return Cache::get('ActusGlobal');
}
protected static function setCachedActus()
{
$actus = Actu::with('tags')->whereStatus(true)->whereFront(true)
->orderBy('updated_at', 'DESC')->limit(3)->get();
Cache::put('ActusGlobal', $actus);
}
}