*/ protected $fillable = [ 'name', 'logo', 'responsable', 'url', 'informations', 'employees', 'latitude', 'longitude', 'activity_id', 'sector_id', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::created(function () { self::resetCache(); }); static::updated(function () { self::resetCache(); }); static::deleted(function () { self::resetCache(); }); static::restored(function () { self::resetCache(); }); static::forceDeleted(function () { self::resetCache(); }); } /** * Reset all cache elements related to this model. * * @return void */ public static function resetCache(): void { Cache::forget('allCompanies'); self::getAllCompanies(); Cache::forget('publishedCompanies'); self::getPublishedCompanies(); } /** * Get all companies. * * @return \Illuminate\Database\Eloquent\Collection */ public static function getAllCompanies(): \Illuminate\Database\Eloquent\Collection { return Cache::remember('allCompanies', 86400, function () { return self::query()->with(['companyActivity', 'companySector'])->get(); }); } /** * Get all published companies. * * @return \Illuminate\Database\Eloquent\Collection */ public static function getPublishedCompanies(): \Illuminate\Database\Eloquent\Collection { return Cache::remember('publishedCompanies', 86400, function () { return self::getAllCompanies()->where('published', true); }); } // * RELATIONSHIPS /** * Get the activity for this company. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\CompanyActivity,$this> */ public function companyActivity(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CompanyActivity::class, 'activity_id'); } /** * Get the sector for this company. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\CompanySector,$this> */ public function companySector(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CompanySector::class, 'sector_id'); } }