ldc-legaulois/app/Models/Articles.php
Jérémy Munsch 34da0f98a1 tons of fix
2021-05-05 15:13:33 +02:00

81 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ArticleCategory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Articles extends Model
{
use CastsEnums;
protected $enumCasts = [
'type' => ArticleCategory::class
];
protected $fillable = [
'title',
'slug',
'content',
'type',
'question',
'multiple_response',
'stop_poll',
'status'
];
protected static function booted()
{
static::creating(function ($article) {
$article->content = clean($article->content);
});
static::updating(function ($article) {
$article->content = clean($article->content);
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::updated(function ($article) {
Cache::delete('actusGlobal');
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::deleted(function ($article) {
Cache::delete('actusGlobal');
});
}
public function recipes()
{
return $this->belongsToMany(Recipes::class);
}
public function answers()
{
return $this->hasMany(SurveysAnswers::class, 'article_id');
}
public function notifiable()
{
return $this->morphOne(Notifications::class, 'notifiable');
}
/**
* Get cached actus
*
* @return \App\Models\Articles
*/
public static function getCachedActus()
{
if (!Cache::has('actusGlobal')) {
self::setCachedActus();
}
return Cache::get('actusGlobal');
}
protected static function setCachedActus()
{
$actus = Articles::where([['status', true], ['type', ArticleCategory::actu()]])
->orderBy('updated_at', 'DESC')->limit(3)->get();
Cache::put('actusGlobal', $actus);
}
}