Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global settings function #158

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace App\Filament\Resources\SettingResource\Pages;

use Illuminate\Support\Facades\Cache;
use App\Filament\Resources\SettingResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateSetting extends CreateRecord
{
protected static string $resource = SettingResource::class;

protected function afterCreate(): void
{
Cache::forget('wave_settings');
}
}
6 changes: 6 additions & 0 deletions app/Filament/Resources/SettingResource/Pages/EditSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\SettingResource\Pages;

use Illuminate\Support\Facades\Cache;
use App\Filament\Resources\SettingResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
Expand All @@ -16,4 +17,9 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function afterSave(): void
{
Cache::forget('wave_settings');
}
}
6 changes: 6 additions & 0 deletions app/Filament/Resources/SettingResource/Pages/ListSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\SettingResource\Pages;

use Illuminate\Support\Facades\Cache;
use App\Filament\Resources\SettingResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
Expand All @@ -16,4 +17,9 @@ protected function getHeaderActions(): array
Actions\CreateAction::make(),
];
}

protected function afterDelete(): void
{
Cache::forget('wave_settings');
}
}
2 changes: 1 addition & 1 deletion resources/themes/anchor/partials/footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@
</ul>
</div>
</x-container>
</footer>
</footer>
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

Route::get('role', function(){
dd(\App\Models\User::find(2)->roles);
});
});
15 changes: 12 additions & 3 deletions wave/src/Helpers/globals.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Cache;

if (!function_exists('setting')) {
function setting($key, $default = null)
{
$value = ($default == null) ? '' : $default;
static $settingsCache = null;

return $value;
// Fetch all settings from cache or database
if ($settingsCache === null) {
$settingsCache = Cache::rememberForever('wave_settings', function () {
return Wave\Setting::pluck('value', 'key')->toArray();
});
}

// Return the requested setting or default value if not found
return $settingsCache[$key] ?? $default;
}
}

Expand Down Expand Up @@ -83,4 +92,4 @@ function get_default_billing_cycle(){
// Return null or a default value if neither is present
return 'Monthly'; // or any default value you prefer
}
}
}
21 changes: 21 additions & 0 deletions wave/src/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wave;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;

class Setting extends Model
Expand All @@ -12,4 +13,24 @@ class Setting extends Model

public $timestamps = false;

protected static function booted()
{
static::saved(function () {
Cache::forget('wave_settings');
});

static::deleted(function () {
Cache::forget('wave_settings');
});
}

public static function get($key, $default = null)
{
$settings = Cache::rememberForever('wave_settings', function () {
return self::pluck('value', 'key')->toArray();
});

return $settings[$key] ?? $default;
}

}
Loading