refactor: ♻️ set global trait for model cache
This commit is contained in:
parent
4dd92ef167
commit
70f21e1971
13 changed files with 151 additions and 301 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<string, string>
|
||||
*/
|
||||
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<int,self>
|
||||
*/
|
||||
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<int,self>
|
||||
*/
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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<int,self>
|
||||
*/
|
||||
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<int,self>
|
||||
*/
|
||||
public static function getPublishedCompanyActivities(): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('publishedCompanyActivities', 86400, function () {
|
||||
return self::getAllCompanyActivities()->where('published', true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -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<int,self>
|
||||
*/
|
||||
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<int,self>
|
||||
*/
|
||||
public static function getPublishedCompanySectors(): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('publishedCompanySectors', 86400, function () {
|
||||
return self::getAllCompanySectors()->where('published', true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -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<string, string>
|
||||
*/
|
||||
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<int,self>
|
||||
*/
|
||||
public static function getDynamicPages(): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('dynamicPages', 86400, function () {
|
||||
return self::query()
|
||||
->with(['parentPage', 'childPages', 'config'])
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
// * RELATIONSHIPS
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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<int,self>
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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<int,self>
|
||||
*/
|
||||
public static function getPublishedPageConfigs(): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('publishedPageConfigs', 86400, function () {
|
||||
return self::query()
|
||||
->with('pages')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
// * RELATIONSHIPS
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
||||
|
|
|
|||
83
app/Traits/Models/HasCache.php
Normal file
83
app/Traits/Models/HasCache.php
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace App\Traits\Models;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* If there is a static function in a trait (named boot[TraitName]),
|
||||
* it will be executed as the boot() function would on an Eloquent model.
|
||||
* Futhermore, event in the Model observer doesn't erase actual events in the model,
|
||||
* but adds them.
|
||||
*/
|
||||
trait HasCache
|
||||
{
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function bootHasCache(): 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
|
||||
{
|
||||
$class = static::class;
|
||||
$cacheKey = static::getCacheKey();
|
||||
|
||||
Cache::forget($cacheKey);
|
||||
|
||||
static::getModelsCached();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all models cached.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection<int,self>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue