Site settings working
This commit is contained in:
parent
220dbbcd52
commit
83e9c8fbd5
|
|
@ -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'];
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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');
|
||||
}
|
||||
};
|
||||
|
|
@ -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-slot:page_title>Test Page</x-slot:page_title>
|
||||
|
||||
@php
|
||||
// $flash = Session::get('_flash');
|
||||
// $messages = $flash['new'];
|
||||
$messages = getMessages();
|
||||
@endphp
|
||||
@php( Settings::set('auditionName','Somewhere Band Directors Association'))
|
||||
@php( Settings::set('auditionAbbreviation','SBDA'))
|
||||
|
||||
|
||||
@foreach($messages as $message)
|
||||
The message "{{ $message['message'] }}" is of type "{{ $message['type'] }}"<br />
|
||||
@endforeach
|
||||
{{ Settings::get('auditionName') }}
|
||||
|
||||
</x-layout.app>
|
||||
d
|
||||
|
|
|
|||
Loading…
Reference in New Issue