generated from spatie/package-skeleton-laravel
-
-
Couldn't load subscription status.
- Fork 0
HasSettings Trait
Azizul Hakim edited this page Jun 12, 2025
·
1 revision
Access settings directly from your Eloquent models with a clean, object-oriented approach.
Add the trait to any model:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Ahs12\Setanjo\Traits\HasSettings;
class User extends Authenticatable
{
use HasSettings;
}$user = User::find(1);
// Set individual settings
$user->settings()->set('theme', 'dark');
$user->settings()->set('language', 'en');
// Method chaining
$user->settings()
->set('theme', 'dark')
->set('language', 'en')
->set('timezone', 'America/New_York');
// Get with defaults
$theme = $user->settings()->get('theme', 'light');
$language = $user->settings()->get('language', 'en');
// Get all settings
$allSettings = $user->settings()->all();class UserPreferencesController extends Controller
{
public function update(Request $request)
{
$user = auth()->user();
$user->settings()
->set('theme', $request->theme)
->set('language', $request->language)
->set('timezone', $request->timezone)
->set('email_notifications', $request->boolean('email_notifications'));
return back()->with('success', 'Preferences updated!');
}
public function show()
{
$user = auth()->user();
return view('preferences', [
'theme' => $user->settings()->get('theme', 'light'),
'language' => $user->settings()->get('language', 'en'),
'timezone' => $user->settings()->get('timezone', 'UTC'),
]);
}
}// Company model
class Company extends Model
{
use HasSettings;
}
// Usage
$company = Company::find(1);
$company->settings()
->set('subscription_plan', 'enterprise')
->set('user_limit', 100)
->set('api_enabled', true);
$plan = $company->settings()->get('subscription_plan', 'basic');Add convenience methods to your models:
class User extends Authenticatable
{
use HasSettings;
public function getTheme(): string
{
return $this->settings()->get('theme', 'light');
}
public function setTheme(string $theme): void
{
$this->settings()->set('theme', $theme);
}
public function hasEmailNotifications(): bool
{
return $this->settings()->get('email_notifications', true);
}
}
// Usage
$user->setTheme('dark');
$theme = $user->getTheme();
if ($user->hasEmailNotifications()) {
// Send notification
}@php
$user = auth()->user();
$theme = $user->settings()->get('theme', 'light');
@endphp
<html data-theme="{{ $theme }}">
{{-- Your content --}}
</html>class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'preferences' => [
'theme' => $this->settings()->get('theme', 'light'),
'language' => $this->settings()->get('language', 'en'),
'timezone' => $this->settings()->get('timezone', 'UTC'),
],
];
}
}All data types are automatically handled:
$user->settings()->set('is_active', true); // Boolean
$user->settings()->set('login_count', 42); // Integer
$user->settings()->set('success_rate', 85.5); // Float
$user->settings()->set('preferences', [ // Array
'theme' => 'dark',
'language' => 'en'
]);
// Retrieved with correct types
$isActive = $user->settings()->get('is_active'); // boolean
$loginCount = $user->settings()->get('login_count'); // integer
$preferences = $user->settings()->get('preferences'); // arraypublic function test_user_can_manage_settings()
{
$user = User::factory()->create();
$user->settings()->set('theme', 'dark');
$this->assertEquals('dark', $user->settings()->get('theme'));
$this->assertEquals('light', $user->settings()->get('nonexistent', 'light'));
}The trait provides a clean, intuitive way to work with settings directly from your models, making your code more readable and maintainable.
Made with ❤️ by @AHS12 • © 2025 • MIT Licensed
- Home - Homepage of the wiki
- Installation Guide - Set up Setanjo in your Laravel project
- Setup & Configuration - Configure tenancy modes and basic options
- Quick Start Examples - Get up and running in minutes
- Tenancy Modes - Understanding strict vs polymorphic modes
- Facade API Reference - Complete method documentation
- HasSettings Trait - Uses of HasSrttings trait
- Commands - Artisan commands for management
- Performance Tips - Optimization best practices