Laravel 12 JWT Settings & Config API – Step by Step

Laravel 12 JWT Settings & Config API – Step by Step

In this guide, we’ll build a Settings & Config API in Laravel 12 using JWT authentication.

We’ll store app configuration as key/value pairs in a settings table and expose CRUD endpoints for easy management.

API Endpoints

Method Endpoint Purpose
GET /api/settings List all settings
GET /api/settings/{key} Get a specific setting
POST /api/settings Create a new setting
PUT /api/settings/{key} Update setting
DELETE /api/settings/{key} Delete setting

šŸ”¹ Step 1: Create Model & Migration

php artisan make:model Setting -m

Migration: create_settings_table.php

public function up()
{
    Schema::create('settings', function (Blueprint $table) {
        $table->id();
        $table->string('key')->unique();
        $table->text('value')->nullable();
        $table->timestamps();
    });
}

Run migration:

php artisan migrate

šŸ”¹ Step 2: Setting Model

app/Models/Setting.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    protected $fillable = ['key', 'value'];

    // Cast JSON values if stored as JSON
    protected $casts = [
        'value' => 'array',
    ];
}

šŸ”¹ Step 3: Create Controller

php artisan make:controller Api/SettingController --api

app/Http/Controllers/Api/SettingController.php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;

class SettingController extends Controller
{
    public function index()
    {
        return response()->json(Setting::all(), 200);
    }

    public function show($key)
    {
        $setting = Setting::where('key', $key)->firstOrFail();
        return response()->json($setting, 200);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'key' => 'required|string|unique:settings,key',
            'value' => 'nullable',
        ]);

        $setting = Setting::create($validated);
        return response()->json($setting, 201);
    }

    public function update(Request $request, $key)
    {
        $setting = Setting::where('key', $key)->firstOrFail();

        $validated = $request->validate([
            'value' => 'nullable',
        ]);

        $setting->update($validated);
        return response()->json($setting, 200);
    }

    public function destroy($key)
    {
        $setting = Setting::where('key', $key)->firstOrFail();
        $setting->delete();

        return response()->json(['message' => 'Setting deleted'], 200);
    }
}

šŸ”¹ Step 4: Define Routes

routes/api.php

use App\Http\Controllers\Api\SettingController;

Route::middleware('auth:api')->group(function () {
    Route::get('/settings', [SettingController::class, 'index']);
    Route::get('/settings/{key}', [SettingController::class, 'show']);
    Route::post('/settings', [SettingController::class, 'store']);
    Route::put('/settings/{key}', [SettingController::class, 'update']);
    Route::delete('/settings/{key}', [SettingController::class, 'destroy']);
});

šŸ”¹ Step 5: Example Usage (Postman)

1. List Settings
GET /api/settings

[
  { "key": "site_name", "value": "My Laravel App" },
  { "key": "logo_url", "value": "https://example.com/logo.png" }
]

2. Create Setting
POST /api/settings

{
  "key": "site_name",
  "value": "My Laravel API"
}

3. Get Specific Setting
GET /api/settings/site_name

{
  "key": "site_name",
  "value": "My Laravel API"
}

4. Update Setting
PUT /api/settings/site_name

{
  "value": "Updated API App"
}

5. Delete Setting
DELETE /api/settings/site_name

šŸ”¹ Step 6: Optimizing with a Helper

Create app/Helpers/SettingHelper.php

use App\Models\Setting;

if (!function_exists('setting')) {
    function setting($key, $default = null)
    {
        return optional(Setting::where('key', $key)->first())->value ?? $default;
    }
}

Register in composer.jsonautoload:

"autoload": {
    "files": [
        "app/Helpers/SettingHelper.php"
    ]
}

Run:

composer dump-autoload

Now you can call:

$siteName = setting('site_name', 'Default App');

šŸŽÆ Conclusion

You now have a Settings & Config API in Laravel 12 with JWT that:

✅ Stores settings in DB (easy to update)
✅ Provides CRUD endpoints
✅ Can serve as a headless settings system for frontend apps
✅ Includes a setting() helper for quick usage in Laravel

This makes your Laravel project more dynamic, configurable, and frontend-ready šŸš€

Want the full source code?

Download the complete Laravel 12 JWT API Authentication example on my GitHub repo here.

Happy Coding!

Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close