Site settings working

This commit is contained in:
Matt Young 2024-06-01 12:32:15 -05:00
parent 220dbbcd52
commit 83e9c8fbd5
4 changed files with 90 additions and 10 deletions

View File

@ -0,0 +1,14 @@
<?php
//Set a setting: Settings::set('name','value')
//Get a setting: Settings::get('name', 'optional: default value')
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SiteSetting extends Model
{
protected $table = 'site_settings';
public $timestamps = false;
protected $fillable = ['setting_key','setting_value'];
}

43
app/Settings.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace App;
use App\Models\SiteSetting;
use Illuminate\Support\Facades\Cache;
class Settings
{
protected static $cacheKey = 'site_settings';
// Load settings from the database and cache them
public static function loadSettings()
{
$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray();
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
}
// Get a setting value by key
public static function get($key, $default = null)
{
$settings = Cache::get(self::$cacheKey, []);
return $settings[$key] ?? $default;
}
// Set a setting value by key
public static function set($key, $value)
{
// Update the database
SiteSetting::updateOrCreate(['setting_key' => $key], ['setting_value' => $value]);
// Update the cache
$settings = Cache::get(self::$cacheKey, []);
$settings[$key] = $value;
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
}
// Clear the cache
public static function clearCache()
{
Cache::forget(self::$cacheKey);
}
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('site_settings', function (Blueprint $table) {
$table->id();
$table->string('setting_key')->unique();
$table->text('setting_value');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('site_settings');
}
};

View File

@ -1,16 +1,11 @@
@php use App\Models\Audition;use App\Models\School;use App\Models\SchoolEmailDomain;use App\Models\User;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Session; @endphp @php use App\Models\Audition;use App\Models\School;use App\Models\SchoolEmailDomain;use App\Models\User;use App\Settings;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Session; @endphp
<x-layout.app> <x-layout.app>
<x-slot:page_title>Test Page</x-slot:page_title> <x-slot:page_title>Test Page</x-slot:page_title>
@php @php( Settings::set('auditionName','Somewhere Band Directors Association'))
// $flash = Session::get('_flash'); @php( Settings::set('auditionAbbreviation','SBDA'))
// $messages = $flash['new'];
$messages = getMessages();
@endphp
{{ Settings::get('auditionName') }}
@foreach($messages as $message)
The message "{{ $message['message'] }}" is of type "{{ $message['type'] }}"<br />
@endforeach
</x-layout.app> </x-layout.app>
d