skip to Main Content

i want to create setting model LIKE tcg/voyager to can use like this *** setting(‘site.title’) *** . how to generate ?
iwant to save to db.
like tcg/voyager .

thanks.

2

Answers


  1. You may create custom configuration files in the config/ folder of your Laravel project. These files should return an array where the key is the option name and the value is the value of the option.

    <?php
    
    // ... config/site.php
    
    return [
    
        'title' => 'My Site'
    
    ];
    

    As per the documentation:

    You may easily access your configuration values using the global config function from anywhere in your application. The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist.

    $title = config('site.title'); // 'My Site'
    
    $description = config('site.description', 'My Default Description'); // My Default Description
    
    Login or Signup to reply.
  2. I did the same not so long ago, it is fairly easy.

    1. Inside composer.json "autoload" add "files": ["app/helpers.php"]

    2. Create file in that directory

    3. Create a table in DB called i.e. settings with columns key and value

    4. Write a function i.e. setting($your_key, $defaultValue = ”) which will query table settings where ‘key’ = $your_key and return value if row exists, if not return $defaultValue

    In blade you can call it same as Voyager setting {{setting('key', '')}}

    You will need to run composer again so that your autoload file will be recognized

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search