Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chori.av's avatar

A way to store site settings in Laravel

I have a landing page, that needs some settings, managed by admins from admin-panel.

What is the good way to implement it? Maybe, some kind of key-value pair database or something like that?

0 likes
14 replies
tisuchi's avatar

If it is after logged in as an Admin, I always prefer to store in DB.

3 likes
chori.av's avatar

@tisuchi, thank you for your reply!

Yes, it happens after logged in as an Admin. Well, will be MySQL suitable for my needs? I mean, I heard of some key-value pair databases like Memcached or Redis, but don't fully understand their usecases)

Can you (or somebody) explain, please - is this the case to use those or not?

Much appreciate :)

2 likes
tisuchi's avatar

@chori.av

Since it's somehow related to configuration, I suggest you to store in .env that is the way more secure and best practice.

3 likes
chori.av's avatar

@tisuchi, well, on the other hand is the fact, that site is multilingual and for each language version there are may be different settings.

Also, I forgot to mention (sorry), that some settings are like "Welcome message" are don't suitable to .env file

But the idea is good, I thought about it too)

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

What I can think of right now is-

  • Store everything in DB.
  • Separate the configuration data and regular admin information.
  • Fetch configuration data from db and store as an environment variable.
  • Created another config file for storing regular admin information.
6 likes
reza305's avatar

@tisuchi If we are going to store data in config files, why do we need to store them in database in the first place? we need to query the database for any recent updates every time we are going to access data from config files.

2 likes
chori.av's avatar

@tisuchi, wow, this is so cool! ?

But could you explain a little (or give link do docs) two things:

  • How can I store something in environment variable
  • And how can I create another config file? (sounds like It something like second .env, but more specific and with another name :D)
2 likes
nolros's avatar

@chori.av 4 Options, are as follows:

Option 1 - traditional method would be settings DB a "settings" table with columns for each setting (doesn't scale well and would definitely NOT recommend)

list of table columns (+1 column for every type of setting)

id: 
is_business: boolean
has_fax: boolean
server: ...

Option 2. DB with 2 tables:

Table 1. setting_prop (options) e.g. is business or has business related to business, but you might have settings like, has_fax, is_local, etc.

id: 3
prop_key: is_business
prop_value: true

id: 4
prop_key: is_business
prop_value: false

Unlike #1 ... you have to search for the key and then read values e.g. (where->("prop_key","is_business")->get()) => in our case we get 2 records back

Table 3: user to setting property relationship (pivot table) - user_id , setting_prop_id e.g. user 1 has selected is_business as false

Option 2. - (My preferred method) json string stored in SQL 5.7 json column or < SQL 5.7 text column. In this case you are parsing the json string to set and get values. So more code than DB approach. Very flexible as you can adjust as you need. Could encrypt string if there is sensitive data contained. Also works great with multi-language as you can create your own language file to support all languages. So each admin will have a record / row. Columns:

 id: int
settings: json / text
timestamp: timestamp  

Option 3. if performance is key you could store as bits. That does require more coding and easy t get confused. Unless you have a large app and high transactions wouldn't recommend.

Example of json once parsed to array

<?php

use App\User;

class Settings
{

    /**
     * @var array
     */
    protected $settings = [];

    /**
     * @var User
     */
    protected $user;

    /**
     * @var array
     */
    protected $defaults = [
        'opt_in_monthly' => false,
        'opt_in_partner' => false,
    ];

    /**
     * Settings constructor.
     * @param array $settings
     * @param User $user
     */
    public function __construct(array $settings, User $user)
    {
        $this->user = $user;
        $this->settings = $settings;
    }

    /**
     * Get setting by key
     *
     * @param $key
     * @return mixed
     */
    public function get($key)
    {
        return array_get($this->settings, $key);
    }

    /**
     * Return true if settings has key
     *
     * @param $key
     * @return bool
     */
    public function has($key)
    {
        return array_key_exists($key,$this->settings);
    }

    /**
     * Set default settings for user
     */
    public function defaults()
    {
        foreach ($this->defaults as $key => $value) {
            $this->user->set($key, $value);
        }
    }

    /**
     * Set setting key value pair
     *
     * @param $key
     * @param $value
     */
    public function set($key, $value)
    {
        $this->settings[$key] = $value;

        $this->persist();
    }

    /**
     * Return all settings
     *
     * @return array
     */
    public function all()
    {
        return $this->settings;
    }

    /**
     * Merge setting attributes with existing attributes
     *
     * @param array $attributes
     * @return bool|int
     */
    public function merge(array $attributes)
    {
        $this->settings = array_merge(
            $this->settings,
            array_only($attributes, array_keys($this->settings))

        );

        return $this->persist();
    }

    /**
     * Save settings
     *
     * @return bool|int
     */
    public function persist()
    {
       return $this->user->update(['settings' => $this->settings]);
    }

    /**
     * Magic get for settings by key
     *
     * @param $key
     * @return mixed
     * @throws \Exception
     */
    public function __get($key)
    {
        if ($this->has($key)) {
            return $this->get($key);
        }

        throw new \Exception("The {$key} setting does not exist.");
    }


}
5 likes
chori.av's avatar

@nolros, thank you for your reply! I will definitely have this four options in mind. It was pretty detailed response :)

2 likes
obrunopolo's avatar

If anyone is interested, I have developed a package for this purpose

Search in packagist for arandu/laravel-site-options (cant post links yet)

1 like

Please or to participate in this conversation.