ldc-legaulois/app/Models/Moments.php
2021-04-28 13:57:12 +02:00

58 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Moments extends Model
{
protected static function booted()
{
static::updated(function ($moment) {
if (Cache::has('momentFiche')) {
/** @var Moments $cachedMoment */
$cachedMoment = Cache::get('momentFiche');
if ($cachedMoment->id == $moment->id && !$moment->star) {
Cache::forget('momentFiche');
}
if ($moment->star) {
Cache::put('momentFiche', $moment);
}
}
});
static::deleted(function ($moment) {
if (Cache::has('momentFiche')) {
/** @var Moments $cachedMoment */
$cachedMoment = Cache::get('momentFiche');
if ($cachedMoment->id == $moment->id) {
Cache::forget('momentFiche');
}
}
});
}
public function getRouteKeyName()
{
return "slug";
}
public function strates()
{
return $this->hasMany(MomentStrates::class, 'moment_id');
}
/**
* Get cached star moment
*
* @return \App\Models\Moments
*/
public static function getCachedStar()
{
if (!Cache::has('momentFiche')) {
$momentFiche = Moments::where([['star_moment', 1], ['status', 1]])->first();
Cache::put('momentFiche', $momentFiche);
}
return Cache::get('momentFiche');
}
}