Sitemap

Member-only story

Store config in database in Laravel

3 min readJun 17, 2024

--

I am currently using Laravel 5.2. I want to be able to store config properties (key-value pairs) in database, that I am willing to access from both my application on runtime and the console (either php artisan command or Tinker).

What is my best option?

  • .env is one way, but it is not stored in the database, but in a file.
  • Config::get is another way, but it also writes in files. Can it be configured to write in database?
  • Cache::get is setup to work with the database, but is temporary, not permanent, so it is out of question.

The reason I am interested in database config, is because we often replace/delete files during deployment. Also it would be nice to store values encrypted. Also important feature here is to be able to easily get values via either php artisan or tinker

Solution

  1. Make a migration: php artisan make:migration CreateSettingsTable
<?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('key');
$table->string('value');
$table->timestamps();

$table->unique([
'key'…

--

--

Ted James
Ted James

Written by Ted James

The world is my office, and every destination is my inspiration