read config from database

This commit is contained in:
Edward Yang 2021-12-29 10:42:49 +08:00
parent 2e6dc556b0
commit 71be6d9bfd
4 changed files with 59 additions and 1 deletions

13
app/Models/Setting.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Setting extends Model
{
use HasFactory;
protected $table = 'settings';
protected $fillable = ['name', 'value', 'json'];
}

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use App\Models\Setting;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -23,6 +24,15 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
$settingsFromDB = Setting::all(['name', 'value', 'json'])
->keyBy('name')
->transform(function ($setting) {
if ($setting->json) {
return \json_decode($setting->value, true);
}
return $setting->value;
})
->toArray();
config(['global' => $settingsFromDB]);
}
}

View File

@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"ext-json": "*",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.65",

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('value');
$table->boolean('json')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}