Skip to content

Commit

Permalink
Merge pull request #158 from thedevdojo/fix-settings-global-func
Browse files Browse the repository at this point in the history
Fix global settings function
  • Loading branch information
tnylea authored Nov 14, 2024
2 parents afc1a51 + 024bd21 commit 0bee6db
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
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;
}

}

0 comments on commit 0bee6db

Please sign in to comment.