64 lines
1.8 KiB
PHP
64 lines
1.8 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::creating(function ($moment) {
|
|
$moment->featured_text = clean($moment->featured_text);
|
|
});
|
|
static::updating(function ($moment) {
|
|
$moment->featured_text = clean($moment->featured_text);
|
|
});
|
|
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');
|
|
}
|
|
}
|