ldc-legaulois/app/Models/Articles.php

82 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2021-04-26 10:29:34 +02:00
use App\Enums\ArticleCategory;
use Illuminate\Database\Eloquent\Model;
2021-04-28 13:57:12 +02:00
use Illuminate\Support\Facades\Cache;
2021-04-26 10:29:34 +02:00
use Kwaadpepper\Enum\Traits\CastsEnums;
class Articles extends Model
{
2021-04-26 10:29:34 +02:00
use CastsEnums;
protected $enumCasts = [
'type' => ArticleCategory::class
];
protected $fillable = [
'title',
'slug',
'content',
'type',
'question',
'multiple_response',
'stop_poll',
2021-04-30 21:17:41 +02:00
'status'
2021-04-26 10:29:34 +02:00
];
2021-04-28 13:57:12 +02:00
protected static function booted()
{
2021-05-05 15:13:33 +02:00
static::creating(function ($article) {
$article->content = clean($article->content);
});
static::updating(function ($article) {
$article->content = clean($article->content);
});
2021-04-28 13:57:12 +02:00
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::updated(function ($article) {
2021-05-01 14:49:25 +02:00
Cache::delete('actusGlobal');
2021-04-28 13:57:12 +02:00
});
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
static::deleted(function ($article) {
2021-05-01 14:49:25 +02:00
Cache::delete('actusGlobal');
2021-04-28 13:57:12 +02:00
});
}
public function recipes()
{
return $this->belongsToMany(Recipes::class);
}
public function answers()
{
return $this->hasMany(SurveysAnswers::class, 'article_id');
}
2021-03-20 14:38:15 +01:00
public function notifiable()
{
return $this->morphOne(Notifications::class, 'notifiable');
}
2021-04-28 13:57:12 +02:00
/**
* 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);
}
}