Skip to content

Commit 024bd21

Browse files
committed
Fix global settings function
1 parent b8aa3ed commit 024bd21

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

app/Filament/Resources/SettingResource/Pages/CreateSetting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
namespace App\Filament\Resources\SettingResource\Pages;
44

5+
use Illuminate\Support\Facades\Cache;
56
use App\Filament\Resources\SettingResource;
67
use Filament\Actions;
78
use Filament\Resources\Pages\CreateRecord;
89

910
class CreateSetting extends CreateRecord
1011
{
1112
protected static string $resource = SettingResource::class;
13+
14+
protected function afterCreate(): void
15+
{
16+
Cache::forget('wave_settings');
17+
}
1218
}

app/Filament/Resources/SettingResource/Pages/EditSetting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Resources\SettingResource\Pages;
44

5+
use Illuminate\Support\Facades\Cache;
56
use App\Filament\Resources\SettingResource;
67
use Filament\Actions;
78
use Filament\Resources\Pages\EditRecord;
@@ -16,4 +17,9 @@ protected function getHeaderActions(): array
1617
Actions\DeleteAction::make(),
1718
];
1819
}
20+
21+
protected function afterSave(): void
22+
{
23+
Cache::forget('wave_settings');
24+
}
1925
}

app/Filament/Resources/SettingResource/Pages/ListSettings.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Resources\SettingResource\Pages;
44

5+
use Illuminate\Support\Facades\Cache;
56
use App\Filament\Resources\SettingResource;
67
use Filament\Actions;
78
use Filament\Resources\Pages\ListRecords;
@@ -16,4 +17,9 @@ protected function getHeaderActions(): array
1617
Actions\CreateAction::make(),
1718
];
1819
}
20+
21+
protected function afterDelete(): void
22+
{
23+
Cache::forget('wave_settings');
24+
}
1925
}

resources/themes/anchor/partials/footer.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,4 @@
199199
</ul>
200200
</div>
201201
</x-container>
202-
</footer>
202+
</footer>

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
Route::get('role', function(){
2121
dd(\App\Models\User::find(2)->roles);
22-
});
22+
});

wave/src/Helpers/globals.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
<?php
22

33
use Illuminate\Database\Eloquent\Relations\Relation;
4+
use Illuminate\Support\Facades\Cache;
45

56
if (!function_exists('setting')) {
67
function setting($key, $default = null)
78
{
8-
$value = ($default == null) ? '' : $default;
9+
static $settingsCache = null;
910

10-
return $value;
11+
// Fetch all settings from cache or database
12+
if ($settingsCache === null) {
13+
$settingsCache = Cache::rememberForever('wave_settings', function () {
14+
return Wave\Setting::pluck('value', 'key')->toArray();
15+
});
16+
}
17+
18+
// Return the requested setting or default value if not found
19+
return $settingsCache[$key] ?? $default;
1120
}
1221
}
1322

@@ -83,4 +92,4 @@ function get_default_billing_cycle(){
8392
// Return null or a default value if neither is present
8493
return 'Monthly'; // or any default value you prefer
8594
}
86-
}
95+
}

wave/src/Setting.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wave;
44

5+
use Illuminate\Support\Facades\Cache;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Setting extends Model
@@ -12,4 +13,24 @@ class Setting extends Model
1213

1314
public $timestamps = false;
1415

16+
protected static function booted()
17+
{
18+
static::saved(function () {
19+
Cache::forget('wave_settings');
20+
});
21+
22+
static::deleted(function () {
23+
Cache::forget('wave_settings');
24+
});
25+
}
26+
27+
public static function get($key, $default = null)
28+
{
29+
$settings = Cache::rememberForever('wave_settings', function () {
30+
return self::pluck('value', 'key')->toArray();
31+
});
32+
33+
return $settings[$key] ?? $default;
34+
}
35+
1536
}

0 commit comments

Comments
 (0)