BossBey File Manager
PHP:
8.2.30
OS:
Linux
User:
imagivibe
Root
/
home
/
imagivibe
/
public_html
/
app.imagivibe.com
/
app
/
Domains
/
Entity
/
Concerns
📤 Upload
📝 New File
📁 New Folder
Close
Editing: HasModel.php
<?php declare(strict_types=1); namespace App\Domains\Entity\Concerns; use App\Domains\Entity\Enums\EntityEnum; use App\Domains\Entity\Models\Entity; use App\Helpers\Classes\Helper; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; trait HasModel { use HasStatus; public function model(?bool $fresh = false): Builder|Model|null { return $this->getEntity($fresh)?->firstWhere('key.value', value: $this->name()); } private function getEntity(?bool $fresh = false): ?Collection { $ttl = Helper::appIsNotDemo() ? 10 : 600; // If fresh is true, bypass the cache and fetch directly from the database if ($fresh) { $validEngines = collect(EntityEnum::cases())->pluck('value'); Entity::whereNotIn('key', $validEngines)->delete(); return Entity::whereIn('key', $validEngines)->get(); } // Otherwise, use the cache to store/retrieve the entities return Cache::remember('entities', $ttl, static function () { $validEngines = collect(EntityEnum::cases())->pluck('value'); Entity::whereNotIn('key', $validEngines)->delete(); return Entity::whereIn('key', $validEngines)->get(); }); } }
Save
Cancel