Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

Laraconfig

> 编程语言
Open source

Per-user settings repository system for Laravel

172 stars0 likes1 views
WebsiteGitHub

About

Per-user settings repository system for Laravel

This package has been archived.

Sorry guys and gals, I bit more than I can chew and I'm currently not using this package to justify its support.

I may revisit this in the near future.


Laraconfig

Per-user settings repository system for Laravel.

This package allows users to have settings that can be queried, changed and even updated, effortlessly and quickly.

php
User::find(1)->settings->set('color', 'red');

Requirements

  • Laravel 8.x
  • PHP 8.0 or later

How it works

Laraconfig works extending Laravel relations, and includes a migration system to easily manage them.

Each Setting is just a value, and references a parent "metadata" that contains the information like the type and name, while being linked to a user.

Since Laraconfig uses the Eloquent ORM behind the scenes, getting a one or all settings is totally transparent to the developer.

Quickstart

You can install the package via composer.

composer require darkghosthunter/laraconfig

First, publish and run the migrations. These will add two tables called user_settings and user_settings_metadata. One holds the values per user, the other the metadata of the setting, respectively.

php artisan vendor:publish --provider="DarkGhostHunter\Laraconfig\LaraconfigServiceProvider" --tag="migrations"
php artisan migrate

The migration uses a morph column to connect to the User. You can change it before migrating.

Second, add the HasConfig trait to the User models you want to have settings.

php
namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use DarkGhostHunter\Laraconfig\HasConfig;

class User extends Authenticatable
{
    use HasConfig;
    
    // ...
}

Finally, use the settings:publish artisan command. This will create a settings folder in the root of your project and a users.php file.

php artisan settings:publish

Now, let's create some settings.

Settings Manifest

Laraconfig makes managing user settings globally using a manifest of sorts, the settings/users.php file. You will see a sample setting already written.

php
use DarkGhostHunter\Laraconfig\Facades\Setting;

Setting::name('color')->string();

Creating a setting

To create a setting, use the Setting facade. You can start with setting the name, which must be unique, and then declare the type.

php
use DarkGhostHunter\Laraconfig\Facades\Setting;

Setting::name('dark_mode')->boolean();

Laraconfig is compatible with 7 types of settings, mirroring their PHP native types, along the Collection and Datetime (Carbon) objects.

  • array()
  • boolean()
  • collection()
  • datetime()
  • float()
  • integer()
  • string()

Arrays and Collections are serialized in the database as JSON.

Default value

All settings have a default value of null, but you can use the default() method to set a different initial value.

php
use DarkGhostHunter\Laraconfig\Facades\Setting;

Setting::name('color')->string()->default('black');

You can later revert the value back to the default using setDefault().

Enabled or Disabled

By default, all settings are enabled by default, but you can change this using disabled().

php
Setting::name('color')->disabled();

Enabled or disable is presentational; a disabled setting can still be updated. You can programmatically set a value using setIfEnabled().

Group settings

You can set a group name to a setting. This can be handy when you want to display settings in the frontend in an ordered manner by separating them in groups.

php
Setting::name('color')->group('theme');

Bag

When Laraconfig migrates the new settings, these are created to all models. You can filter a given set of settings through "bags".

By default, all settings are created under the users bag, but you can change the default bag for anything using the bag() method.

php
Setting::name('color')->group('theme')->bag('style');

Setting::name('notify_email')->boolean()->default(true)->bag('notifications');
Setting::name('notify_sms')->boolean()->default(false)->bag('notifications');

Later, in your model, you can filter the bags you want to work with using filterBags() in your model.

Migrating settings

Once you're done creating your settings, you should use settings:migrate to let Laraconfig add the settings metadata to your database.

php artisan settings:migrate

Behind the scenes, Laraconfig will look into your Models for those using the HasConfig trait, and populate the settings accordingly using the information on the manifest.

Migration run only forward. There is no way to revert a migration once done. On production, removing settings needs confirmation.

Adding new settings

Simply create a new setting and run settings:migrate. Existing settings won't be created again, as Laraconfig will check their existence before doing it.

php
use DarkGhostHunter\Laraconfig\Facades\Setting;

Setting::name('color')->string()->default('black');

// This new setting will be created 
Setting::name('notifications')->boolean()->default(true);

Removing old settings

To remove old settings, simply remove their declaration and run settings:migrate. Laraconfig compares the settings declared to the ones created in the database, and removes those that no longer exist in the manifest at the end of the migration execution.

php
use DarkGhostHunter\Laraconfig\Facades\Setting;

// Commenting this line will remove the "color" setting on migration.
// Setting::name('color')->string()->default('black');

// This new setting will be created 
Setting::name('notifications')->boolean()->default(true);

Since this procedure can be dangerous, confirmation will be needed on production environments.

Upgrading settings

You don't need to get directly into the database to update a setting. Instead, just change the setting properties directly in the manifest. Laraconfig will update the metadata accordingly.

Let's say we have a "color" setting we wish to update from a string to an array of colors, with a default and a group.

php
Setting::name('color')->string()->bag('theme');

// This is the new declaration.
// Setting::name('color')
//    ->array()
//    ->default(['black'])
//    ->group('theme');

Laraconfig will detect the new changes, and update the metadata keeping the users value intact.

php
// This is the old declaration.
// Setting::name('color')->string()->bag('theme');

Setting::name('color')
    ->array()
    ->default(['black'])
    ->group('theme');

Updating only occurs if the setting is different from before at migration time.

Once done, we can migrate the old setting to the new one using settings:migrate. Users will keep the same setting value they had, but... What if we want to also change the value for each user? We can use the using() method to feed each user setting to a callback that will return the new value.

php
Setting::name('color')
    ->array()
    ->default('black')
    ->group('theme')
    ->using(fn ($old) => $old->value ?? 'black'); // If the value is null, set it as "black".

The using() method only runs if the setting is different from before at migration time.

Behind the scenes, Laraconfig will look for the "color" setting, update the metadata, and then use a lazy() query to update the value with the callback.

Consider migrating directly on the database if you have hundreds of thousands of records, as this procedure is safer but slower than a direct SQL statement.

Migrating to a new setting

On other occasions, you may want to migrate a setting to a completely new one. In both cases you can use from() to get the old setting value to migrate from, and using() if you want to also update the value of each user.

Taking the same example above, we will migrate the "color" setting to a simple "dark theme" setting.

php
// This old declaration will be deleted after the migration ends.
// Setting::name('color')->string()->bag('theme');

// This is a new setting.
Setting::name('dark')
    ->boolean()
    ->default(false)
    ->group('theme')
    ->from('color')
    ->using(static fn ($old) => $old->value === 'black'); // If it's black, then it's dark.

The from and using are executed only if the old setting exists at migration time.

Behind the scenes, Laraconfig creates the new "theme" setting first, and then looks for the old "color" setting in the database to translate the old values to the new ones. Since the old setting is not present in the manifest, it will be deleted from the database.

Managing Settings

Laraconfig handles settings like any Eloquent Morph-Many Relationship, but supercharged.

Just simply use the settings property on your model. This property is like your normal Eloquent Collection, so you have access to all its tools.

php
$user = User::find(1);

echo "Your color is: {$user->settings->get('color')}.";

Using settings is preferred, as it will load the settings only once.

Initializing

By default, the HasConfig trait will create a new bag of Settings in the database after a User is successfully created through the Eloquent ORM, so you don't have to create any setting.

In case you want to handle initialization manually, you can use the shouldInitializeConfig() method and return false, which can be useful when programmatically initializing the settings.

php
// app/Models/User.php

/**
 * Check if the user should initialize settings automatically after creation.
 * 
 * @return bool
 */
protected function shouldInitializeConfig(): bool
{
    // Don't initialize the settings if the user is not verified from the start.
    // We will initialize them only once the email is properly verified.
    return null !== $this->email_verified_at;
}

Since the user in the example above won't be initialized, we have to do it manually using initialize().

php
// Initialize if not initialized before.
$user->settings()->initialize();

// Forcefully initialize, even if already initialized.
$user->settings()->initialize(true);

Checking settings initialization

You can check if a user configuration has been initialized or not using isInitialized().

php
if ($user->settings()->isInitialized()) {
    return 'You have a config!';
}

Retrieving settings

You can easily get a value of a setting using the name, which makes everything into a single beautiful oneliner.

php
return "Your favorite color is {$user->settings->color}";

Since this only supports alphanumeric and underscore characters, you can use value().

php
return "Your favorite color is {$user->settings->value('color')}";

You can also get the underlying Setting model using get(). If the setting doesn't exist, it will return null.

php
$setting = $user->settings->get('theme');

echo "You're using the [$setting->value] theme.";

Since the settings is a collection, you have access to all the goodies, like iteration:

php
foreach ($user->settings as $setting) {
    echo "The [$setting->name] has the [$setting->value] value.";
}

You can also use the only() method to return a collection of settings by their name, or except() to retrieve all the settings except those issued.

php
$user->settings->only('colors', 'is_dark');

$user->settings->except('dark_mode')

Issues· 10 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言