Laravel Website’s Settings Made Simple: How to Save and Handle Them Efficiently
In the dynamic world of web development, flexibility and maintainability are crucial for building robust applications. Laravel, known for its elegant syntax and powerful tools, offers multiple ways to manage application settings. From handling configurations through JSON files to leveraging database-driven approaches, Laravel provides developers with the tools to balance simplicity and scalability.
But as your project grows, so does the complexity of managing these settings effectively. How do you ensure that your settings are secure, easily editable, and performant for administrators?
This article will explore a practical guide to saving and managing settings in a Laravel project. Whether you’re dealing with global application settings like website titles and descriptions, or specific configurations for loyalty points, we’ll cover step-by-step approaches to make your project scalable and admin-friendly. By the end, you’ll clearly understand how to implement and optimize your settings for both development and production environments.
Let’s dive in!
Step 1: Store Settings in the Database:
Create a migration file that will be used to save settings in the database as a key-value store:
php artisan make:migration create_settings_table
in the generated migration file, add key and value columns like this:
Schema::create('settings', function (Blueprint $table) {
$table->string('key')->unique();
$table->text('value');
$table->timestamps();
});
Step 2: Create Setting Model:
Run the command:
php artisan make:model Setting
Open the generated Setting model file, and add $fillable fields:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['key', 'value'];
}
Step 3: Add Initial Settings By Seeder:
To create a seeder class for settings run this command:
php artisan make:seeder SettingsSeeder
In the SettingsSeeder class, add the settings key-value pairs according to your project needs:
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
]);
}
Then, run the seeder:
php artisan db:seed --class=SettingsSeeder
Step 4: Retrieve Settings from the Database to Access Settings in Your Application:
the database and override the values defined in the config file. You can do this using a service provider:
In your AppServiceProvider(or a custom service provider), add the following in the boot() method:
use App\Models\Setting;
public function boot()
{
if (Schema::hasTable('settings')) {
$settings = Setting::all()->pluck('value', 'key')->toArray();
config(['settings' => $settings]); // Load the settings dynamically into Laravel's config
}
}
This makes all settings accessible via config(‘settings.key’), for example config(‘settings.site_title’).
Congrats! Now, you can create an edit form for admin, for example, that displays the initial seeded settings, where he can update them as needed.
You will need to create the needed controller, routes to handle edit and update methods, and blade view to show the edit form.
This is an example of an edit form in the blade view file:
<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>
Conclusion
By implementing a robust system for handling settings, you not only simplify development but also empower your application to grow alongside your needs. As you continue refining your Laravel project, remember that well-managed settings are the foundation for a streamlined and adaptable application. Take the time to plan and execute this aspect carefully — it will pay dividends in the long run.
With the tools and techniques covered in this guide, you’re well-equipped to make your Laravel settings management both efficient and future-proof.