LoginSignup
5
4

More than 5 years have passed since last update.

Laravel config DBで管理する

Posted at

ある程度migration、model、commandの使い方が分かっている人向け。

migration

php artisan make:migration create_laravel_config_table
database/migrations/xxxx_create_laravel_config_table.phpを編集
Schema::create('laravel_config', function (Blueprint $table) {
    $table->increments('id');
    $table->string('config_name', 16)->comment('設定ファイル名');
    $table->string('config_key', 64)->comment('設定KEY');
    $table->text('config_value')->comment('設定値');
    $table->unsignedTinyInteger('is_json')->default(0)->comment('JSONフラグ');
});

model

php artisan make:model LaravelConfig
app/LaravelConfig.phpを編集
class LaravelConfig extends Model
{
    protected $table = 'laravel_config';
    public $timestamps = false;
}

command

php artisan make:command CreateConfigFile
app/Console/Commands/CreateConfigFile.phpを編集
protected $signature = 'batch:CreateConfigFile';

public function handle()
{
    $configs = [];
    $laravel_config = [];
    $config_data = LaravelConfig::get();
    if ($config_data->isNotEmpty()) {
        $list = $config_data->all();
        foreach ($list as $v) {
            $configs[] = $v->toArray();
        }
    }
    if (!empty($configs)) {
        foreach ($configs as $v) {
            if ($v['is_json'] == 1) {
                $value = json_decode($v['config_value'], true);
            } else {
                $value = $v['config_value'];
            }
            $laravel_config[$v['config_name']][$v['config_key']] = $value;
        }
    }
    if (!empty($laravel_config)) {
        foreach ($laravel_config as $config_name => $config) {
            $file_path = 'config/' . $config_name . '.php';
            $file_data = '<?php' . PHP_EOL;
            $file_data .= '/**' . PHP_EOL;
            $file_data .= '  Auto Created' . PHP_EOL;
            $file_data .= '  ' . date('Y-m-d H:i:s') . PHP_EOL;
            $file_data .= '*/' . PHP_EOL;
            $file_data .= 'return ' . var_export($config, true) . ';' . PHP_EOL;
            file_put_contents($file_path, $file_data);
        }
    }


    return 0;
}

laravel_configにデータを登録し、作成したコマンドを実行するとconfigディレクトリ配下に設定ファイルを作成します。
本番環境ではキャッシュ生成前に実行しておきます。

ただ、これだとファイル管理のままでよくないか…?

動的にconfig設定

これをアプリケーションの共通処理か何かに仕込んでおく。
こちらの方がDB化する意味あるかも。

$configs = [];
$config_data = LaravelConfig::get();
if ($config_data->isNotEmpty()) {
    $list = $config_data->all();
    foreach ($list as $v) {
        $configs[] = $v->toArray();
    }
}
if (!empty($configs)) {
    foreach ($configs as $v) {
        if ($v['is_json'] == 1) {
            $value = json_decode($v['config_value'], true);
        } else {
            $value = $v['config_value'];
        }
        \Config::set($v['config_name'] . '.' . $v['config_key'], $value);
    }
}
5
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4