Custom app settings - Laravel

Custom settings in your Laravel website

You can set custom settings in your Laravel website that will help you manage content of user side from your admin panel. For example, I am managing my website’s title and logo from admin panel. It is also helpful for your clients as well.

Let’s say you build a website for your client and now whenever he needs to update his website’s title and logo, he contacts you and you update it manually. Instead of this, you can give them an option on admin panel so they can do themselves.

1. Creating the table

We will create a table named “settings” where we will store all our custom settings. So run the following command at the root of your project to create a migration:

php artisan make:migration create_settings_table

This will create a file something like “2025_10_09_031642_create_settings_table.php” in your “database/migrations” folder.

In this function, you need to add 2 columns in your “up” function:

  • app_key: Unique key for each setting.
  • app_value: Value of that setting.
public function up(): void
{
    Schema::create('settings', function (Blueprint $table) {
        $table->id();
        $table->string("app_key")->nullable();
        $table->longText("app_value")->nullable();
        $table->timestamps();
    });
}

After that, run the command to create this table:

php artisan migrate

Once the command is executed, check your phpMyAdmin, you will now have a new table “settings” in your database.

2. Saving the settings

In order to save the settings, we will create an API that will be called from your admin panel. So write the following code in your “routes/api.php” file:

use App\Http\Controllers\SettingsController;

Route::post("/save-settings", [SettingsController::class, "save"]);

You can use your own already created controller as well. But if you do not have one, you can create your controller by running the following command:

php artisan make:controller SettingsController

This will create a file “SettingsController.php” inside “app/Http/Controllers” folder. Following will be the code of it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class SettingsController extends Controller
{
    public function save()
    {
        $title = request()->title ?? "";

        $this->save_settings("title", $title);

        return response()->json([
            "status" => "success",
            "message" => "Settings has been saved."
        ]);
    }

    private function save_settings($key, $value)
    {
        DB::table("settings")
            ->updateOrInsert([
                "app_key" => $key
            ], [
                "app_value" => $value,
                "updated_at" => now()->utc()
            ]);

        cache()->forget($key);
    }
}

You can call the “save_settings” function multiple times to save multiple settings. Notice that we are removing the cache once admin updates the settings. This is so that the user can always see the updated value. But there will be no un-necessary read requests to the database.

3. Testing the API

Run the following command in your terminal:

curl -X "POST" -d "title=Adnan" http://localhost:3000/api/save-settings

And you will see the response that “Settings has been saved.”. You can check in your database that a new row should be created in “settings” table. You can try calling the same API with different value, and you will notice that the same row gets updated. So there will be no duplicate rows with same key in “settings” table.

You can also try Postman or VS Code built-in API testing feature. But for the sake of simplicity, you can use CURL command in the terminal.

4. Fetch settings

In order to fetch the settings, we will create a helper function.

  • First, create a folder named “Helpers” inside “app” folder.
  • Then inside that folder, create a file named “helpers.php”.
  • Open your “composer.json” file and add the newly created helper file path in it.
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/Helpers/helpers.php"
    ]
},

Then run the following command in your terminal to regenerate the autoloader files:

composer dump-autoload

Following will be the content of “app/Helpers/helpers.php”:

<?php

function fetch_setting($key)
{
    return cache()->rememberForever($key, function () use ($key) {
        return DB::table("settings")->where("app_key", "=", $key)->value("app_value") ?? "";
    });
}

This function will first check the setting in cache, if found then it will return the cached value. If not found, then it will fetch the latest value of that setting from database.

Conclusion

So that’s how you can create your own custom settings in your Laravel website. Simply create a new table and save all the settings in it. Fetch them using cache for speed.