letsgoi/laravel-settings

Laravel settings package

3.0.1 2024-08-29 13:45 UTC

This package is auto-updated.

Last update: 2024-10-29 14:19:37 UTC


README

Move your env variables to database to change them easily using laravel nova

Requirements

  • PHP >= 8.3
  • Laravel >= 10.0
  • Laravel nova license registered on project

Installation

  • Require package with composer:
composer require letsgoi/laravel-settings
  • Add Letsgoi\LaravelSettings\SettingsServiceProvider::classto app.php
    /*
     * Package Service Providers...
     */
    ...
    Letsgoi\LaravelSettings\SettingsServiceProvider::class,
  • Publish configuration:
php artisan vendor:publish --provider="Letsgoi\LaravelSettings\SettingsServiceProvider" --tag="migrations"
  • Add SettingNovaResource::class on NovaServiceProvider.php
    protected function resources(): void
    {
        Nova::resources([
            ...
            SettingNovaResource::class,
        ]);
    }

Usage

This package use cache memory to save variables, instead of retrieving them from database each time you need to use it:

  1. When you save a variable, cache of this variable is cleared.
  2. First time you retrieve a variable it is saved to cache.

To register a new variable on Settings:

  1. Create a migration with the name and key of the variable:
    1. The available types are: bool, string, float, array
    DB::table('settings')->insert([
            'id' => 'APP_URL',
            'type' => 'string'
    ]);
  1. Run migrations: php artisan migrate
  2. Access your nova url and fill the variable with the value: For booleans use 0 (false) and 1 (true).
  3. Use variable in your code using:
    $settingRepository = new SettingRepository();
    
    $value = $settingRepository->find('key');

Testing

Run tests: composer test