Move your env variables to database to change them easily using laravel nova
- PHP >= 8.3
- Laravel >= 10.0
- Laravel nova license registered on project
- Require package with composer:
composer require letsgoi/laravel-settings
- Add
Letsgoi\LaravelSettings\SettingsServiceProvider::class
toapp.php
/*
* Package Service Providers...
*/
...
Letsgoi\LaravelSettings\SettingsServiceProvider::class,
- Publish configuration:
php artisan vendor:publish --provider="Letsgoi\LaravelSettings\SettingsServiceProvider" --tag="migrations"
- Add
SettingNovaResource::class
onNovaServiceProvider.php
protected function resources(): void
{
Nova::resources([
...
SettingNovaResource::class,
]);
}
This package use cache memory to save variables, instead of retrieving them from database each time you need to use it:
- When you save a variable, cache of this variable is cleared.
- First time you retrieve a variable it is saved to cache.
To register a new variable on Settings:
- Create a migration with the name and key of the variable:
- The available types are: bool, string, float, array
DB::table('settings')->insert([
'id' => 'APP_URL',
'type' => 'string'
]);
- Run migrations:
php artisan migrate
- Access your nova url and fill the variable with the value: For booleans use 0 (false) and 1 (true).
- Use variable in your code using:
$settingRepository = new SettingRepository();
$value = $settingRepository->find('key');
Run tests:
composer test