Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created June 28, 2021 10:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/29a3c31541519df98b9578e73473026b to your computer and use it in GitHub Desktop.
Save martinbean/29a3c31541519df98b9578e73473026b to your computer and use it in GitHub Desktop.
Caching settings repository
<?php
namespace App\Providers;
use App\Repositories\SettingsRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(SettingsRepository::class, function () {
return new SettingsRepository();
});
}
}
<?php
namespace App\Repositories;
use App\Models\Setting;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
class SettingsRepository
{
public function all(): Collection
{
return Cache::rememberForever('settings', function () {
return Setting::pluck('value', 'key');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment