Created
June 28, 2021 10:53
-
-
Save martinbean/29a3c31541519df98b9578e73473026b to your computer and use it in GitHub Desktop.
Caching settings repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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