*/ protected $fillable = [ 'title', 'location', 'location_details', 'location_latitude', 'location_longitude', 'contract', 'contract_period', 'function', 'experience_level', 'qualifications', 'skills', 'picture', 'picture_alt', 'picture_title', 'link', 'description', 'recruiter_firstname', 'recruiter_lastname', 'recruiter_job', 'recruiter_picture', 'published', 'published_at', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'skills' => 'array', 'published' => 'boolean', 'published_at' => 'datetime', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $offer) { $offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true); $offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true); }); static::created(function () { self::resetCache(); }); static::updating(function (self $offer) { $offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true); $offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true); }); static::updated(function (self $offer) { FileStorageHelper::removeOldFile($offer, 'picture'); FileStorageHelper::removeOldFile($offer, 'recruiter_picture'); self::resetCache(); }); static::deleted(function (self $offer) { FileStorageHelper::removeFile($offer, 'picture'); FileStorageHelper::removeFile($offer, 'recruiter_picture'); self::resetCache(); }); } /** * Reset all cache elements related to this model. * * @return void */ private static function resetCache(): void { Cache::forget('offersCached'); Cache::forget('offersCountCached'); self::getOffersCached(); self::getOffersCountCached(); } /** * Get all offers. * * @return \Illuminate\Database\Eloquent\Collection */ public static function getOffersCached(): \Illuminate\Database\Eloquent\Collection { return Cache::remember('offersCached', 86400, function () { return self::query()->where('published', true)->get(); }); } /** * Get offers count. * * @return integer */ public static function getOffersCountCached(): int { return Cache::remember('offersCountCached', 86400, function () { return GeneralSetting::query()->where('type', SettingsType::offersCount)->firstOrFail()->value['count']; }); } }