Managing Laravel Application Settings with a Database-Driven Approach
This guide explains how to create a flexible, maintainable settings system in Laravel by storing configuration key‑value pairs in a database, generating migrations, models, seeders, service‑provider boot logic, and an admin Blade form for editing settings.
When building robust web applications, flexibility and maintainability are essential; Laravel provides elegant syntax and a rich toolset for managing application settings.
Step 1: Store Settings Using Database
Create a migration to hold key‑value pairs for settings.
php artisan make:migration create_settings_tableIn the generated migration file, define the key and value columns:
Schema::create('settings', function (Blueprint $table) {
$table->string('key')->unique();
$table->text('value');
$table->timestamps();
});Step 2: Create a Settings Model
Generate the model with the Artisan command: php artisan make:model Setting Open the generated Setting model and add the $fillable property:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['key', 'value'];
}Step 3: Add Initial Settings with a Seeder
Create a seeder class: php artisan make:seeder SettingsSeeder Inside SettingsSeeder, insert the required key‑value pairs:
use App\Models\Setting;
public function run()
{
Setting::insert([
['key' => 'site_title', 'value' => 'My Awesome Website'],
['key' => 'site_description', 'value' => 'Default website description.'],
['key' => 'website_logo', 'value' => '/images/logo.png'],
['key' => 'points_expiry_days', 'value' => '365'],
// Add other settings as needed
]);
}Run the seeder:
php artisan db:seed --class=SettingsSeederStep 4: Retrieve Settings from the Database
In AppServiceProvider (or a custom service provider), load settings during the boot() method:
use App\Models\Setting;
public function boot()
{
if (Schema::hasTable('settings')) {
$settings = Setting::all()->pluck('value', 'key')->toArray();
config(['settings' => $settings]); // Dynamically load settings into Laravel's config
}
}Now any setting can be accessed via config('settings.key'), e.g., config('settings.site_title').
Finally, create an admin Blade view with a form to edit these settings. Example form:
<form action="{{ route('admin.settings.update') }}" method="POST">
@csrf
<div>
<label for="site_title">Site Title</label>
<input type="text" name="site_title" value="{{ config('settings.site_title') }}" />
</div>
<div>
<label for="site_description">Site Description</label>
<textarea name="site_description" class="form-control">{{ config('settings.site_description') }}</textarea>
</div>
<div>
<label>Current Logo</label>
<img src="{{ Storage::url(config('settings.website_logo')) }}" style="max-width:190px;">
</div>
<div>
<label for="website_logo">Upload New Logo</label>
<input name="website_logo" type="file" id="website_logo" class="form-control">
</div>
<button type="submit">Save Settings</button>
</form>By implementing this database‑driven settings system, you simplify development, improve flexibility, and ensure your Laravel application can adapt to future configuration changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
