1. Go to this page and download the library: Download gissilali/per-user-settings library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gissilali / per-user-settings example snippets
User::find(1)->settings->set('color', 'red');
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DarkGhostHunter\Laraconfig\HasConfig;
class User extends Authenticatable
{
use HasConfig;
// ...
}
use DarkGhostHunter\Laraconfig\Facades\Setting;
Setting::name('color')->string();
use DarkGhostHunter\Laraconfig\Facades\Setting;
Setting::name('dark_mode')->boolean();
use DarkGhostHunter\Laraconfig\Facades\Setting;
Setting::name('color')->string()->default('black');
use DarkGhostHunter\Laraconfig\Facades\Setting;
Setting::name('color')->string()->default('black');
// This new setting will be created
Setting::name('notifications')->boolean()->default(true);
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);
Setting::name('color')->string()->bag('theme');
// This is the new declaration.
// Setting::name('color')
// ->array()
// ->default(['black'])
// ->group('theme');
// This is the old declaration.
// Setting::name('color')->string()->bag('theme');
Setting::name('color')
->array()
->default(['black'])
->group('theme');
Setting::name('color')
->array()
->default('black')
->group('theme')
->using(fn ($old) => $old->value ?? 'black'); // If the value is null, set it as "black".
// 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.
$user = User::find(1);
echo "Your color is: {$user->settings->get('color')}.";
// 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;
}
// Initialize if not initialized before.
$user->settings()->initialize();
// Forcefully initialize, even if already initialized.
$user->settings()->initialize(true);
if ($user->settings()->isInitialized()) {
return 'You have a config!';
}
return "Your favorite color is {$user->settings->color}";
return "Your favorite color is {$user->settings->value('color')}";
$setting = $user->settings->get('theme');
echo "You're using the [$setting->value] theme.";
foreach ($user->settings as $setting) {
echo "The [$setting->name] has the [$setting->value] value.";
}