# How to Use Settings in Your Website

This guide explains how to use the Settings system in your Laravel application.

## Overview

The Settings system allows you to store and retrieve configuration values that can be managed through the Filament admin panel. Settings support different data types: string, text, number, boolean, and JSON.

## Quick Start

### 1. Using the Helper Function (Recommended)

The easiest way to get a setting value is using the `setting()` helper function:

```php
// Get a setting value
$siteName = setting('site_name', 'My Website');

// Get a boolean setting
$maintenanceMode = setting('maintenance_mode', false);

// Get a number setting
$maxPosts = setting('max_posts_per_page', 10);
```

### 2. Using the SettingHelper Class

```php
use App\Helpers\SettingHelper;

// Get a setting
$value = SettingHelper::get('site_name', 'Default Value');

// Set a setting programmatically
SettingHelper::set('site_name', 'My New Site', 'string', 'general', 'The name of the website');

// Get all settings grouped by group
$allSettings = SettingHelper::all();

// Get settings by group
$seoSettings = SettingHelper::group('seo');
```

### 3. Direct Model Usage

```php
use App\Models\Setting;

// Get a setting
$setting = Setting::where('key', 'site_name')->first();
$value = $setting->value; // Automatically cast based on type

// Create/Update a setting
Setting::updateOrCreate(
    ['key' => 'site_name'],
    [
        'value' => 'My Website',
        'type' => 'string',
        'group' => 'general',
        'description' => 'The name of the website'
    ]
);
```

## Usage Examples

### In Blade Templates

```blade
<!-- Get a simple setting -->
<h1>{{ setting('site_name', 'My Website') }}</h1>

<!-- Get a setting with conditional -->
@if(setting('maintenance_mode', false))
    <div class="alert">Site is under maintenance</div>
@endif

<!-- Get settings for SEO -->
<meta name="description" content="{{ setting('meta_description', '') }}">
<meta name="keywords" content="{{ setting('meta_keywords', '') }}">
```

### In Controllers

```php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        return view('home', [
            'siteName' => setting('site_name', 'My Website'),
            'siteDescription' => setting('site_description', ''),
            'maxPosts' => setting('max_posts_per_page', 10),
        ]);
    }
}
```

### In API Controllers

```php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Helpers\SettingHelper;

class SettingController extends Controller
{
    public function index()
    {
        return response()->json([
            'site_name' => setting('site_name'),
            'contact_email' => setting('contact_email'),
            'social_links' => setting('social_links', []), // JSON type
        ]);
    }
}
```

### Working with Different Types

#### String Settings
```php
$siteName = setting('site_name', 'Default Name');
```

#### Number Settings
```php
// In Filament, set type to "number"
$maxPosts = setting('max_posts_per_page', 10); // Returns float/int
```

#### Boolean Settings
```php
// In Filament, set type to "boolean"
$maintenanceMode = setting('maintenance_mode', false); // Returns true/false

if (setting('maintenance_mode', false)) {
    // Site is in maintenance mode
}
```

#### JSON Settings
```php
// In Filament, set type to "JSON" and enter JSON like:
// {"facebook": "https://facebook.com", "twitter": "https://twitter.com"}

$socialLinks = setting('social_links', []); // Returns array

// Access the data
$facebookUrl = $socialLinks['facebook'] ?? null;
```

### Common Settings Examples

Here are some common settings you might want to create:

#### General Settings
- `site_name` (string) - Website name
- `site_description` (text) - Website description
- `contact_email` (string) - Contact email
- `contact_phone` (string) - Contact phone
- `address` (text) - Physical address

#### SEO Settings
- `meta_description` (text) - Default meta description
- `meta_keywords` (text) - Default meta keywords
- `google_analytics_id` (string) - Google Analytics ID

#### Social Media Settings
- `social_links` (json) - Social media links
  ```json
  {
    "facebook": "https://facebook.com/yourpage",
    "twitter": "https://twitter.com/yourhandle",
    "instagram": "https://instagram.com/yourhandle"
  }
  ```

#### Feature Flags
- `maintenance_mode` (boolean) - Enable/disable maintenance mode
- `registration_enabled` (boolean) - Enable/disable user registration
- `comments_enabled` (boolean) - Enable/disable comments

#### Display Settings
- `posts_per_page` (number) - Number of posts per page
- `max_upload_size` (number) - Maximum file upload size in MB
- `timezone` (string) - Application timezone

## Cache Management

Settings are automatically cached for 1 hour to improve performance. The cache is automatically cleared when you update or delete a setting through Filament.

To manually clear the cache:

```php
use App\Helpers\SettingHelper;

// Clear cache for a specific setting
SettingHelper::clearCache('site_name');

// Clear all settings cache
SettingHelper::clearCache();
```

## Best Practices

1. **Use Groups**: Organize settings by group (e.g., 'general', 'seo', 'social', 'email')
2. **Provide Defaults**: Always provide a default value when using `setting()` helper
3. **Use Appropriate Types**: Choose the correct type (string, number, boolean, json) for each setting
4. **Add Descriptions**: Add descriptions in Filament to help remember what each setting does
5. **Cache Awareness**: Remember that settings are cached, so changes might take up to 1 hour to appear (or clear cache manually)

## Managing Settings in Filament

1. Go to `/admin/settings` in your admin panel
2. Click "Create" to add a new setting
3. Fill in:
   - **Key**: Unique identifier (e.g., `site_name`)
   - **Value**: The actual value
   - **Type**: Choose from string, text, number, boolean, or JSON
   - **Group**: Organize settings (e.g., `general`, `seo`, `social`)
   - **Description**: Optional description

4. Click "Save" - the setting is now available throughout your application!

