Skip to content

Setup & Configuration

Azizul Hakim edited this page Jun 12, 2025 · 2 revisions

This guide covers basic and advanced configuration options for Laravel Setanjo.

Quick Setup Options

Option 1: Default Setup (Recommended for Beginners)

No configuration needed! Works out of the box with:

  • Tenancy Mode: Strict (single tenant type)
  • Tenant Model: App\Models\User
  • Caching: Disabled
  • Validation: Enabled
use Ahs12\Setanjo\Facades\Settings;

// Global settings
Settings::set('app_name', 'My App');

// User-specific settings
$user = User::find(1);
Settings::for($user)->set('theme', 'dark');

Option 2: Custom Strict Mode

Perfect for single-model tenancy with a different model:

// config/setanjo.php
return [
    'tenancy_mode' => 'strict',
    'strict_tenant_model' => App\Models\Company::class,
];

Usage:

$company = Company::find(1);
Settings::for($company)->set('plan', 'premium');

Option 3: Polymorphic Mode

For complex multi-tenant applications:

// config/setanjo.php
return [
    'tenancy_mode' => 'polymorphic',
    'allowed_tenant_models' => [
        App\Models\User::class,
        App\Models\Company::class,
        App\Models\Organization::class,
    ],
];

Usage:

$user = User::find(1);
$company = Company::find(1);

Settings::for($user)->set('theme', 'dark');
Settings::for($company)->set('plan', 'enterprise');

Configuration File

Publish the configuration file to customize Setanjo:

php artisan vendor:publish --tag="setanjo-config"

Complete Configuration Reference

<?php
// config/setanjo.php

return [
    /*
    |--------------------------------------------------------------------------
    | Repository Driver
    |--------------------------------------------------------------------------
    */
    'repository' => env('SETANJO_REPOSITORY', 'database'),

    /*
    |--------------------------------------------------------------------------
    | Database Settings
    |--------------------------------------------------------------------------
    */
    'table' => env('SETANJO_TABLE', 'settings'),

    /*
    |--------------------------------------------------------------------------
    | Tenantable Column Configuration
    |--------------------------------------------------------------------------
    | Base name for polymorphic columns: {tenantable_column}_type/_id
    */
    'tenantable_column' => 'tenantable',

    /*
    |--------------------------------------------------------------------------
    | Tenancy Mode
    |--------------------------------------------------------------------------
    | 'strict' - Single tenant model type
    | 'polymorphic' - Multiple tenant model types
    */
    'tenancy_mode' => env('SETANJO_TENANCY_MODE', 'strict'),

    /*
    |--------------------------------------------------------------------------
    | Strict Tenant Model (strict mode only)
    |--------------------------------------------------------------------------
    */
    'strict_tenant_model' => env('SETANJO_STRICT_TENANT_MODEL', 'App\\Models\\User'),

    /*
    |--------------------------------------------------------------------------
    | Allowed Tenant Models (polymorphic mode only)
    |--------------------------------------------------------------------------
    */
    'allowed_tenant_models' => [
        // App\Models\Company::class,
        // App\Models\User::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Cache Configuration
    |--------------------------------------------------------------------------
    */
    'cache' => [
        'enabled' => env('SETANJO_CACHE_ENABLED', false),
        'store' => env('SETANJO_CACHE_STORE', null), // null = default store
        'prefix' => env('SETANJO_CACHE_PREFIX', 'setanjo'),
        'ttl' => env('SETANJO_CACHE_TTL', 3600), // 1 hour
    ],

    /*
    |--------------------------------------------------------------------------
    | Validation
    |--------------------------------------------------------------------------
    */
    'validation' => [
        'enabled' => env('SETANJO_VALIDATION_ENABLED', true),
        'throw_exceptions' => env('SETANJO_THROW_EXCEPTIONS', true),
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Settings
    |--------------------------------------------------------------------------
    | Installed via: php artisan setanjo:install-defaults
    */
    'defaults' => [
        'app_name' => [
            'value' => 'Laravel Application',
            'description' => 'Application name',
        ],
        'maintenance_mode' => [
            'value' => false,
            'description' => 'Application maintenance mode',
        ],
    ],
];

Tenancy Mode Configuration

Strict Mode

Best for applications with a single tenant model type:

'tenancy_mode' => 'strict',
'strict_tenant_model' => App\Models\User::class,

Pros:

  • Simpler configuration
  • Better performance (no model class resolution)
  • Type safety for tenant operations

Cons:

  • Limited to one tenant model type
  • Requires reconfiguration to add new tenant types

Polymorphic Mode

Best for complex multi-tenant applications:

'tenancy_mode' => 'polymorphic',
'allowed_tenant_models' => [
    App\Models\User::class,
    App\Models\Company::class,
    App\Models\Organization::class,
],

Pros:

  • Multiple tenant model types
  • Flexible tenant hierarchy
  • Perfect for SaaS applications

Cons:

  • Slightly more complex setup
  • Requires model class specification for some operations

Caching Configuration

Enable Caching

'cache' => [
    'enabled' => true,
    'store' => 'redis', // or 'file', 'database', etc.
    'prefix' => 'myapp_settings',
    'ttl' => 7200, // 2 hours
],

Cache Stores

Redis (Recommended for production):

'cache' => [
    'enabled' => true,
    'store' => 'redis',
    'ttl' => 3600,
],

Database Cache:

'cache' => [
    'enabled' => true,
    'store' => 'database',
    'ttl' => 1800,
],

File Cache:

'cache' => [
    'enabled' => true,
    'store' => 'file',
    'ttl' => 3600,
],

Validation Configuration

Enable/Disable Validation

'validation' => [
    'enabled' => true,           // Validate tenant models
    'throw_exceptions' => true,  // Throw exceptions on validation failure
],

Disable Validation (Not Recommended)

'validation' => [
    'enabled' => false,
    'throw_exceptions' => false,
],

Database Configuration

Custom Table Name

'table' => 'application_settings',

Custom Column Names

'tenantable_column' => 'owner', // Creates: owner_type, owner_id

Environment-Based Configuration

Development Environment

SETANJO_CACHE_ENABLED=false
SETANJO_VALIDATION_ENABLED=true
SETANJO_THROW_EXCEPTIONS=true

Production Environment

SETANJO_CACHE_ENABLED=true
SETANJO_CACHE_STORE=redis
SETANJO_CACHE_TTL=7200
SETANJO_VALIDATION_ENABLED=true

Testing Environment

SETANJO_CACHE_ENABLED=false
SETANJO_VALIDATION_ENABLED=false
SETANJO_THROW_EXCEPTIONS=false

Default Settings Installation

Configure Default Settings

// config/setanjo.php
'defaults' => [
    'app_name' => [
        'value' => 'My Laravel App',
        'description' => 'Application name displayed in header',
    ],
    'user_registration' => [
        'value' => true,
        'description' => 'Allow new user registration',
    ],
    'api_rate_limit' => [
        'value' => 1000,
        'description' => 'API requests per hour limit',
    ],
],

Install Default Settings

# Install defaults (skip existing)
php artisan setanjo:install-defaults

# Force reinstall (overwrite existing)
php artisan setanjo:install-defaults --force

Advanced Configuration Examples

Multi-Database Setup

// Different connection for settings
'connection' => 'settings_db',

Custom Repository

// Custom repository implementation
'repository' => 'custom',

// In service provider
$this->app->bind(SettingsRepositoryInterface::class, MyCustomRepository::class);

Performance Optimization

'cache' => [
    'enabled' => true,
    'store' => 'redis',
    'prefix' => 'app_settings',
    'ttl' => 86400, // 24 hours for rarely changing settings
],

// Disable validation in production for better performance
'validation' => [
    'enabled' => env('APP_ENV') !== 'production',
    'throw_exceptions' => true,
],

Configuration Validation

Test your configuration:

php artisan tinker
// Test global settings
Settings::set('test_key', 'test_value');
Settings::get('test_key'); // Should return 'test_value'

// Test tenant settings (strict mode)
$user = User::first();
Settings::for($user)->set('user_test', 'user_value');
Settings::for($user)->get('user_test'); // Should return 'user_value'

// Test caching (if enabled)
Settings::set('cache_test', 'cached_value');
// Check your cache store to verify the key exists

Next Steps

📚 Documentation Structure

Home

  • Home - Homepage of the wiki

Getting Started

Core Concepts

API Reference

Advanced Usage

  • Commands - Artisan commands for management

Tips

🤝 Community

Clone this wiki locally