update enum with cache

This commit is contained in:
Vincent BRIDIER 2023-08-08 09:32:56 +02:00
parent 103ff22d5b
commit 4476166c9e
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
3 changed files with 12 additions and 19 deletions

View file

@ -7,6 +7,7 @@ use App\Enums\Department;
use App\Http\Requests\ContactFormRequest;
use App\Mail\SendContactMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Cache;
class ContactController extends Controller
{
@ -17,7 +18,15 @@ class ContactController extends Controller
*/
public function contact()
{
return view('front.contact');
$listCountries = Cache::remember('listCountries', 86400 , function () {
return Country::toArray();
});
$listDeps = Cache::remember('listDeps', 86400 , function () {
return Department::toArray();
});
return view('front.contact', compact('listCountries', 'listDeps'));
}
/**

View file

@ -4,20 +4,9 @@ namespace App\Providers;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use App\Enums\Country;
use Illuminate\Support\Facades\Cache;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
@ -34,10 +23,5 @@ class AppServiceProvider extends ServiceProvider
}
return request()->server('HTTP_X_FORWARDED_FOR') ?? request()->getClientIp();
});
$listCountries = Cache::remember('listCountries', 60, function () {
return Country::toArray();
});
view()->share('availableCountries', $listCountries ?? Country::toArray());
}
}

View file

@ -112,7 +112,7 @@
<div class="country">
<select id="country" class="changeLength" maxlength="255" name="country" required>
<option value="" @if(!old('country')) selected @endif>{{ __('Sélectionnez un pays') }} *</option>
@foreach($availableCountries as $country)
@foreach($listCountries as $country)
<option value="{{ $country->value }}" @if(old('country') == $country->value) selected @endif>{{ __($country->label) }}</option>
@endforeach
</select>
@ -120,7 +120,7 @@
<div class="drom">
<select id="departement" class="changeLength" maxlength="255" name="departement">
<option value="" @if(!old('departement')) selected @endif>{{ __('Sélectionnez un département') }} *</option>
@foreach (\App\Enums\Department::toArray() as $departement)
@foreach ($listDeps as $departement)
<option value="{{ $departement->value }}" @if(old('departement') === $departement->value) selected @endif>{{ __($departement->label) }}</option>
@endforeach
</select>