diff --git a/app/Http/Controllers/Back/CompanyController.php b/app/Http/Controllers/Back/CompanyController.php index fb2b473e..a137b001 100644 --- a/app/Http/Controllers/Back/CompanyController.php +++ b/app/Http/Controllers/Back/CompanyController.php @@ -78,8 +78,8 @@ class CompanyController extends Controller */ public function create(Company $company): \Inertia\Response { - $companyActivityModels = CompanyActivity::getAllCompanyActivities(); - $companySectorModels = CompanySector::getAllCompanySectors(); + $companyActivityModels = CompanyActivity::getModelsCached(); + $companySectorModels = CompanySector::getModelsCached(); return Inertia::render('Companies/Create', [ 'data' => CommonResource::make($company), 'companyActivityModels' => $companyActivityModels, @@ -133,8 +133,8 @@ class CompanyController extends Controller */ public function edit(Company $company): \Inertia\Response { - $companyActivityModels = CompanyActivity::getAllCompanyActivities(); - $companySectorModels = CompanySector::getAllCompanySectors(); + $companyActivityModels = CompanyActivity::getModelsCached(); + $companySectorModels = CompanySector::getModelsCached(); return Inertia::render('Companies/Edit', [ 'data' => CommonResource::make($company), 'companyActivityModels' => $companyActivityModels, diff --git a/app/Http/Controllers/Back/DynamicPageController.php b/app/Http/Controllers/Back/DynamicPageController.php index 66908258..ab3bb5de 100644 --- a/app/Http/Controllers/Back/DynamicPageController.php +++ b/app/Http/Controllers/Back/DynamicPageController.php @@ -82,8 +82,8 @@ class DynamicPageController extends Controller */ public function create(DynamicPage $dynamic_page): \Inertia\Response { - $dynamicPageModels = DynamicPage::getDynamicPages(); - $pageConfigModels = PageConfig::getPublishedPageConfigs(); + $dynamicPageModels = DynamicPage::getModelsCached(); + $pageConfigModels = PageConfig::getModelsCached(); return Inertia::render('DynamicPages/Create', [ 'data' => CommonResource::make($dynamic_page), 'dynamicPageModels' => $dynamicPageModels, @@ -137,10 +137,10 @@ class DynamicPageController extends Controller public function edit(DynamicPage $dynamic_page): \Inertia\Response { $dynamicPageModel = $dynamic_page->load(['parentPage', 'config']); - $dynamicPageModels = DynamicPage::getDynamicPages() + $dynamicPageModels = DynamicPage::getModelsCached() ->filter(fn ($model) => $model->getKey() !== $dynamic_page->getKey()) ->values(); - $pageConfigModels = PageConfig::getPublishedPageConfigs(); + $pageConfigModels = PageConfig::getModelsCached(); return Inertia::render('DynamicPages/Edit', [ 'data' => CommonResource::make($dynamicPageModel), 'dynamicPageModels' => $dynamicPageModels, diff --git a/app/Models/Company.php b/app/Models/Company.php index 3fcc7946..1d7f789d 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Models\CompanyActivity; use App\Models\CompanySector; +use App\Traits\Models\HasCache; use App\Traits\Models\HasUwilImages; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -31,6 +32,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog; class Company extends Model { use ActivityLog; + use HasCache; use HasFactory; use SoftDeletes; use HasUwilImages; @@ -62,68 +64,15 @@ class Company extends Model protected $casts = [ 'published' => 'boolean', ]; - + /** - * Perform any actions required after the model boots. + * Relations should be included in the cache. * - * @return void + * @var array */ - 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); - }); - } + protected static $cacheRelations = [ + 'companyActivity', + ]; // * RELATIONSHIPS diff --git a/app/Models/CompanyActivity.php b/app/Models/CompanyActivity.php index 7508f4bc..5064437f 100644 --- a/app/Models/CompanyActivity.php +++ b/app/Models/CompanyActivity.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Contracts\TranslatableModel; use App\Helpers\TranslationHelper; +use App\Traits\Models\HasCache; use App\Traits\Models\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -23,6 +24,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog; class CompanyActivity extends Model implements TranslatableModel { use ActivityLog; + use HasCache; use HasFactory; use HasTranslations; use SoftDeletes; @@ -66,24 +68,9 @@ class CompanyActivity extends Model implements TranslatableModel static::creating(function (self $model) { self::manageFields($model); }); - static::created(function (self $model) { - self::resetCache(); - }); static::updating(function (self $model) { self::manageFields($model); }); - static::updated(function () { - self::resetCache(); - }); - static::deleted(function () { - self::resetCache(); - }); - static::restored(function () { - self::resetCache(); - }); - static::forceDeleted(function () { - self::resetCache(); - }); } // * METHODS @@ -114,42 +101,4 @@ class CompanyActivity extends Model implements TranslatableModel } TranslationHelper::validateFallbackTranslations($model); } - - /** - * Reset all cache elements related to this model. - * - * @return void - */ - public static function resetCache(): void - { - Cache::forget('allCompanyActivities'); - self::getAllCompanyActivities(); - - Cache::forget('publishedCompanyActivities'); - self::getPublishedCompanyActivities(); - } - - /** - * Get all company activities. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getAllCompanyActivities(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('allCompanyActivities', 86400, function () { - return self::query()->get(); - }); - } - - /** - * Get all published company activities. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getPublishedCompanyActivities(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('publishedCompanyActivities', 86400, function () { - return self::getAllCompanyActivities()->where('published', true); - }); - } } \ No newline at end of file diff --git a/app/Models/CompanySector.php b/app/Models/CompanySector.php index 1132b199..5650f029 100644 --- a/app/Models/CompanySector.php +++ b/app/Models/CompanySector.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Contracts\TranslatableModel; use App\Helpers\TranslationHelper; +use App\Traits\Models\HasCache; use App\Traits\Models\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -22,6 +23,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog; class CompanySector extends Model implements TranslatableModel { use ActivityLog; + use HasCache; use HasFactory; use HasTranslations; use SoftDeletes; @@ -64,24 +66,9 @@ class CompanySector extends Model implements TranslatableModel static::creating(function (self $model) { self::manageFields($model); }); - static::created(function (self $model) { - self::resetCache(); - }); static::updating(function (self $model) { self::manageFields($model); }); - static::updated(function () { - self::resetCache(); - }); - static::deleted(function () { - self::resetCache(); - }); - static::restored(function () { - self::resetCache(); - }); - static::forceDeleted(function () { - self::resetCache(); - }); } // * METHODS @@ -112,42 +99,4 @@ class CompanySector extends Model implements TranslatableModel } TranslationHelper::validateFallbackTranslations($model); } - - /** - * Reset all cache elements related to this model. - * - * @return void - */ - public static function resetCache(): void - { - Cache::forget('allCompanySectors'); - self::getAllCompanySectors(); - - Cache::forget('publishedCompanySectors'); - self::getPublishedCompanySectors(); - } - - /** - * Get all company sectors. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getAllCompanySectors(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('allCompanySectors', 86400, function () { - return self::query()->get(); - }); - } - - /** - * Get all published company sectors. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getPublishedCompanySectors(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('publishedCompanySectors', 86400, function () { - return self::getAllCompanySectors()->where('published', true); - }); - } } \ No newline at end of file diff --git a/app/Models/DynamicPage.php b/app/Models/DynamicPage.php index 0319543c..1268ae61 100644 --- a/app/Models/DynamicPage.php +++ b/app/Models/DynamicPage.php @@ -6,6 +6,7 @@ use App\Contracts\TranslatableModel; use App\Enums\DynamicFieldTypes; use App\Enums\DynamicPages\PageStatusEnum; use App\Helpers\TranslationHelper; +use App\Traits\Models\HasCache; use App\Traits\Models\HasTranslations; use App\Traits\Models\HasUwilImages; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -34,6 +35,7 @@ use Uwil\LaravelCommon\Helpers\FileHelper; class DynamicPage extends Model implements TranslatableModel { use ActivityLog; + use HasCache; use HasFactory; use HasTranslations; use HasUwilImages; @@ -76,6 +78,17 @@ class DynamicPage extends Model implements TranslatableModel 'title', 'content', ]; + + /** + * Relations should be included in the cache. + * + * @var array + */ + protected static $cacheRelations = [ + 'config', + 'parentPage', + 'childPages', + ]; /** * Perform any actions required after the model boots. @@ -88,22 +101,15 @@ class DynamicPage extends Model implements TranslatableModel self::manageFields($model); $model->id_key = $model->slug; }); - static::created(function () { - self::resetCache(); - }); static::updating(function (self $model) { self::manageFields($model); self::checkParentsStatus($model); }); static::updated(function (self $model) { - self::resetCache(); self::setAllChildPageStatus($model); // TODO: update this after recursive deleting images. // DynamicPageHelper::removeImgs($model); }); - static::deleted(function () { - self::resetCache(); - }); static::restoring(function (self $model) { if ($model->config_id && $model->config()->onlyTrashed()->exists()) { throw ValidationException::withMessages([ @@ -114,11 +120,7 @@ class DynamicPage extends Model implements TranslatableModel } $model->status = PageStatusEnum::draft; }); - static::restored(function () { - self::resetCache(); - }); static::forceDeleted(function (self $model) { - self::resetCache(); // TODO: update this after recursive deleting images. // DynamicPageHelper::removeImgs($model, true); }); @@ -269,31 +271,6 @@ class DynamicPage extends Model implements TranslatableModel return $view; } - /** - * Reset all cache elements related to this model. - * - * @return void - */ - public static function resetCache(): void - { - Cache::forget('dynamicPages'); - self::getDynamicPages(); - } - - /** - * Get all published dynamic pages. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getDynamicPages(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('dynamicPages', 86400, function () { - return self::query() - ->with(['parentPage', 'childPages', 'config']) - ->get(); - }); - } - // * RELATIONSHIPS /** diff --git a/app/Models/News.php b/app/Models/News.php index 4b186f9a..f896bd8d 100644 --- a/app/Models/News.php +++ b/app/Models/News.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Contracts\TranslatableModel; use App\Enums\News\NewsStatusEnum; use App\Helpers\TranslationHelper; +use App\Traits\Models\HasCache; use App\Traits\Models\HasTranslations; use App\Traits\Models\HasUwilImages; use Carbon\Carbon; @@ -36,6 +37,7 @@ use Illuminate\Support\Facades\Cache; class News extends Model implements TranslatableModel { use ActivityLog; + use HasCache; use HasFactory; use HasTranslations; use SoftDeletes; @@ -119,25 +121,12 @@ class News extends Model implements TranslatableModel static::creating(function (self $model) { self::manageFields($model); }); - static::created(function (self $model) { - self::resetCache(); - }); static::updating(function (self $model) { self::manageFields($model); }); - static::updated(function (self $model) { - self::resetCache(); - }); - static::deleted(function () { - self::resetCache(); - }); static::restoring(function (self $model) { - self::resetCache(); $model->status = NewsStatusEnum::unpublished; }); - static::forceDeleted(function (self $model) { - self::resetCache(); - }); } // * METHODS @@ -236,32 +225,6 @@ class News extends Model implements TranslatableModel return NewsStatusEnum::unpublished; } - /** - * Reset all cache elements related to this model. - * - * @return void - */ - public static function resetCache(): void - { - Cache::forget('getPublishedNews'); - self::getPublishedNews(); - } - - /** - * Get all published news. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getPublishedNews(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('getPublishedNews', 86400, function () { - return self::query() - ->where('status', NewsStatusEnum::published) - ->with('categories') - ->get(); - }); - } - /** * Get formated published_at date. * diff --git a/app/Models/PageConfig.php b/app/Models/PageConfig.php index 096935c1..6de235c8 100644 --- a/app/Models/PageConfig.php +++ b/app/Models/PageConfig.php @@ -5,11 +5,13 @@ namespace App\Models; use App\Contracts\TranslatableModel; use App\Enums\DynamicPages\PageStatusEnum; use App\Helpers\TranslationHelper; +use App\Traits\Models\HasCache; use App\Traits\Models\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Str; use Uwil\LaravelActivityLogs\Traits\ActivityLog; /** @@ -23,6 +25,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog; class PageConfig extends Model implements TranslatableModel { use ActivityLog; + use HasCache; use HasFactory; use HasTranslations; use SoftDeletes; @@ -74,21 +77,11 @@ class PageConfig extends Model implements TranslatableModel static::creating(function (self $model) { self::manageFields($model); }); - static::created(function () { - self::resetCache(); - }); static::updating(function (self $model) { self::manageFields($model); }); - static::updated(function (self $model) { - self::resetCache(); - }); static::deleted(function (self $model) { $model->pages()->delete(); - self::resetCache(); - }); - static::restored(function () { - self::resetCache(); }); static::forceDeleting(function (self $model) { $pages = $model->pages()->withTrashed()->get(); @@ -96,9 +89,6 @@ class PageConfig extends Model implements TranslatableModel $page->forceDelete(); }); }); - static::forceDeleted(function () { - self::resetCache(); - }); } // * METHODS @@ -155,31 +145,6 @@ class PageConfig extends Model implements TranslatableModel return true; } - /** - * Reset all cache elements related to this model. - * - * @return void - */ - public static function resetCache(): void - { - Cache::forget('publishedPageConfigs'); - self::getPublishedPageConfigs(); - } - - /** - * Get all published dynamic pages. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getPublishedPageConfigs(): \Illuminate\Database\Eloquent\Collection - { - return Cache::remember('publishedPageConfigs', 86400, function () { - return self::query() - ->with('pages') - ->get(); - }); - } - // * RELATIONSHIPS /** diff --git a/app/Providers/DynamicPageRouteServiceProvider.php b/app/Providers/DynamicPageRouteServiceProvider.php index 9e1d3707..3e7890b8 100644 --- a/app/Providers/DynamicPageRouteServiceProvider.php +++ b/app/Providers/DynamicPageRouteServiceProvider.php @@ -27,7 +27,7 @@ class DynamicPageRouteServiceProvider extends ServiceProvider } // Load all published dynamic pages with each parent page relationship. - $dynamicPageModels = DynamicPage::getDynamicPages() + $dynamicPageModels = DynamicPage::getModelsCached() ->where('status', PageStatusEnum::published) ->load('parentPage'); diff --git a/app/Traits/Models/HasCache.php b/app/Traits/Models/HasCache.php new file mode 100644 index 00000000..56d42fa9 --- /dev/null +++ b/app/Traits/Models/HasCache.php @@ -0,0 +1,83 @@ + + */ + public static function getModelsCached(): \Illuminate\Database\Eloquent\Collection + { + $class = static::class; + $cacheKey = static::getCacheKey(); + + return Cache::remember($cacheKey, 86400, function () use ($class) { + /** @var \Illuminate\Database\Eloquent\Model $class */ + return $class::query() + ->when(property_exists($class, 'cacheRelations') && is_array($class::$cacheRelations), function ($query) use ($class) { + $query->with($class::$cacheRelations); + }) + ->get(); + }); + } + + /** + * Generate a unique cache key for the model. + * + * @return string + */ + protected static function getCacheKey(): string + { + return 'cache_' . str_replace('\\', '_', static::class); + } +} diff --git a/app/View/Components/Front/Pages/KnowUs/OurImplantations.php b/app/View/Components/Front/Pages/KnowUs/OurImplantations.php index f2707b86..c6dd8aee 100644 --- a/app/View/Components/Front/Pages/KnowUs/OurImplantations.php +++ b/app/View/Components/Front/Pages/KnowUs/OurImplantations.php @@ -19,12 +19,15 @@ class OurImplantations extends Component */ public function __construct($id, $implantations) { - $this->id = $id; - $this->implantations = $implantations; - - $this->companyModels = Company::getPublishedCompanies()->whereNotNull('sector_id')->values(); - - $this->companyActivityModels = CompanyActivity::getPublishedCompanyActivities()->values(); + $this->id = $id; + $this->implantations = $implantations; + $this->companyModels = Company::getModelsCached() + ->where('published', true) + ->whereNotNull('sector_id') + ->values(); + $this->companyActivityModels = CompanyActivity::getModelsCached() + ->where('published', true) + ->values(); } /** diff --git a/database/migrations/2025_11_26_173420_create_companies.php b/database/migrations/2025_11_26_173420_create_companies.php index 13ca6c7d..aa32199c 100644 --- a/database/migrations/2025_11_26_173420_create_companies.php +++ b/database/migrations/2025_11_26_173420_create_companies.php @@ -106,11 +106,11 @@ return new class extends Migration $companyModel->latitude = $value['latitude']; $companyModel->longitude = $value['longitude']; $companyModel->employees = isset($value['effectif']) && !empty($value['effectif']) ? (int) $value['effectif'] : null; - $companyModel->activity_id = CompanyActivity::getAllCompanyActivities() + $companyModel->activity_id = CompanyActivity::getModelsCached() ->where('name', Str::ucFirst($value['type_activite'])) ->first() ->getKey(); - $companyModel->sector_id = isset($value['filiere']) ? CompanySector::getAllCompanySectors() + $companyModel->sector_id = isset($value['filiere']) ? CompanySector::getModelsCached() ->where('name', Str::ucFirst($value['filiere'])) ->first() ->getKey() : null; diff --git a/routes/breadcrumbs-front.php b/routes/breadcrumbs-front.php index 0e7859fc..4b53e4ab 100644 --- a/routes/breadcrumbs-front.php +++ b/routes/breadcrumbs-front.php @@ -54,7 +54,10 @@ Breadcrumbs::for('front.search', function (Generator $trail) { }); // * ESPACE PRESSE. -$pageEspacePresse = DynamicPage::getDynamicPages()->where('status', PageStatusEnum::published)->where('id_key', 'espace-presse')->first(); +$pageEspacePresse = DynamicPage::getModelsCached() + ->where('status', PageStatusEnum::published) + ->where('id_key', 'espace-presse') + ->first(); if ($pageEspacePresse) { Breadcrumbs::for('front.dynamic-pages.espace-presse.fr', function (Generator $trail) use ($pageEspacePresse) { $trail->parent('front.homepage'); @@ -67,7 +70,7 @@ if ($pageEspacePresse) { } // * ENGAGEMENTS PAGES. -$pageOurCommitments = DynamicPage::getDynamicPages() +$pageOurCommitments = DynamicPage::getModelsCached() ->where('status', PageStatusEnum::published) ->where('id_key', 'nos-engagements') ->first(); @@ -109,7 +112,7 @@ Breadcrumbs::for('front.bien-nourrir', function (Generator $trail) { }); // * KNOW US PAGES. -$pageKnowUs = DynamicPage::getDynamicPages() +$pageKnowUs = DynamicPage::getModelsCached() ->where('status', PageStatusEnum::published) ->where('id_key', 'nous-connaitre') ->first(); @@ -123,7 +126,10 @@ if ($pageKnowUs) { } // * OUR BRANDS PAGE. -$pageNosMarques = DynamicPage::getDynamicPages()->where('status', PageStatusEnum::published)->where('id_key', 'nos-marques')->first(); +$pageNosMarques = DynamicPage::getModelsCached() + ->where('status', PageStatusEnum::published) + ->where('id_key', 'nos-marques') + ->first(); if ($pageNosMarques) { Breadcrumbs::for('front.dynamic-pages.nos-marques.fr', function (Generator $trail) use ($pageNosMarques) { $trail->parent('front.homepage'); @@ -136,7 +142,10 @@ if ($pageNosMarques) { } // * JOIN US. -$pageNousRejoindre = DynamicPage::getDynamicPages()->where('status', PageStatusEnum::published)->where('id_key', 'nous-rejoindre')->first(); +$pageNousRejoindre = DynamicPage::getModelsCached() + ->where('status', PageStatusEnum::published) + ->where('id_key', 'nous-rejoindre') + ->first(); if ($pageNousRejoindre) { Breadcrumbs::for('front.dynamic-pages.nous-rejoindre.fr', function (Generator $trail) use ($pageNousRejoindre) { $trail->parent('front.homepage'); @@ -149,7 +158,10 @@ if ($pageNousRejoindre) { } // * OUR JOBS. -$pageNosMetiers = DynamicPage::getDynamicPages()->where('status', PageStatusEnum::published)->where('id_key', 'nos-metiers')->first(); +$pageNosMetiers = DynamicPage::getModelsCached() + ->where('status', PageStatusEnum::published) + ->where('id_key', 'nos-metiers') + ->first(); if ($pageNosMetiers) { Breadcrumbs::for('front.dynamic-pages.nos-metiers.fr', function (Generator $trail) use ($pageNosMetiers) { $trail->parent('front.homepage');