dongyu8694 2015-12-10 17:48
浏览 39

Laravel 5.1通过db设置配置

In laravel there are some config files which could be controlled / set with .env file for developing. My question is it possible to set the production values through a db table for example a settings_mail and settings_app table. I need this so that the admin when nessesery could change Some value in a view and doesn't need entering the code

  • 写回答

1条回答 默认 最新

  • dongxiong4571 2015-12-10 19:23
    关注

    Sure. Create a settings table, like this:

    +------------+--------------------+-------------------+
    |   name     |        value       |      category     |
    +------------+--------------------+---------+---------+
    | hostname   | smpt.example.com   |        mail       |
    +------------+--------------------+-------------------+
    |  address   |       San José     |      general      |
    +------------+--------------------+-------------------+
    

    You create a setting model based in table structure:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Setting extends Model
    {
        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'settings';
    
        protected $name = '';
        protected $value = '';
        protected $category = '';
    }
    

    Load the settings to be accessible at entire application:

    // app/Providers/AppServiceProvider.php
    public function boot(){
        $settings = App\Setting::lists('name', 'value')->all();
        config()->set('settings', $settings);
    }
    

    Now, get some config:

    $address = config('settings.address');
    
    评论

报告相同问题?