*/ protected $fillable = [ 'title', 'picture', 'picture_alt', 'picture_title', 'jobs_list', 'job_url', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'jobs_list' => 'array', 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $profession) { $profession->picture = FileStorageHelper::storeFile($profession, $profession->picture, true); }); static::created(function () { self::resetCache(); }); static::updating(function (self $profession) { $profession->picture = FileStorageHelper::storeFile($profession, $profession->picture, true); }); static::updated(function (self $profession) { FileStorageHelper::removeOldFile($profession, 'picture'); self::resetCache(); }); static::deleted(function (self $profession) { FileStorageHelper::removeFile($profession, 'picture'); self::resetCache(); }); } // * METHODS /** * Reset all cache elements related to this model. * * @return void */ private static function resetCache() { Cache::forget('5publishedProfessions'); self::getRandomPublished(); } /** * Get 5 published professions. */ public static function getRandomPublished(): Collection { return Cache::remember('5publishedProfessions', 86400, function () { return self::query()->where('published', true)->inRandomOrder()->get(); }); } }