F
Filamentβ€’2y ago
usmcgator

Write to cache after submit

I created a --simple filament resource. How do I write the values from the create/edit form to cache after it gets updated in the database?
11 Replies
toeknee
toekneeβ€’2y ago
How do you mean to cache?
usmcgator
usmcgatorOPβ€’2y ago
I'd like to write the value to cache to reduce load, if not in cache, it will read from the DB. This is what I'd like to do after it submits the form: Cache::forever('start_date', $record->start_date);
toeknee
toekneeβ€’2y ago
Not that I know off, it's kinda the whole point of Livewire and Filament. You'd need more of a database cache solution.
usmcgator
usmcgatorOPβ€’2y ago
I could set this value in the .env file which will get cached, but I want to give admins the ability to set the value using a form, which in turn writes to the file cache so I can just reference the cache value instead of doing a DB call every time.
toeknee
toekneeβ€’2y ago
Hang onn, so you don't want to actually cache it you want to update a file? Don't use the .env then and instead write to the database in config/ and in there they are cached after called. Then you can clear the config cache on updating. Here is an example of using a service provider to fetch the settings from the db that will then be cached
// app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Config;
use App\Models\Setting; // Assuming you have a 'Setting' model

//...

public function boot()
{
$this->loadSettingsFromDatabase();
}

private function loadSettingsFromDatabase()
{
try {
$settings = Setting::all();

foreach ($settings as $setting) {
Config::set("settings.{$setting->key}", $setting->value);
}
} catch (\Exception $e) {
// Log an error message or handle the exception as appropriate
}
}
// app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Config;
use App\Models\Setting; // Assuming you have a 'Setting' model

//...

public function boot()
{
$this->loadSettingsFromDatabase();
}

private function loadSettingsFromDatabase()
{
try {
$settings = Setting::all();

foreach ($settings as $setting) {
Config::set("settings.{$setting->key}", $setting->value);
}
} catch (\Exception $e) {
// Log an error message or handle the exception as appropriate
}
}
usmcgator
usmcgatorOPβ€’2y ago
I just don't want to make a DB call for a value used often, so I wanted to cache it in the file cache and then use Cache::get('start_date') to get the value when needed for a query. If that value is not in cache, then get it from the DB. Something like that the resource model is actually Settings πŸ™‚ This looks like it'll work. So will I be able to get the value using Cache::get('start_date')?
toeknee
toekneeβ€’2y ago
You use the above, then just call it with config so: config('settings.the_field_value') config is cached at boot
wyChoong
wyChoongβ€’2y ago
Is this for managing settings?
wyChoong
wyChoongβ€’2y ago
Save yourself some time use the spatie setting plugin https://filamentphp.com/docs/2.x/spatie-laravel-settings-plugin/installation
Filament
Installation - Spatie Settings Plugin - Filament
A plugin to add support for spatie/laravel-settings to Filament.
wyChoong
wyChoongβ€’2y ago
Set it up according to filament and spatie doc and configure the caching https://github.com/spatie/laravel-settings#caching-settings
GitHub
GitHub - spatie/laravel-settings: Store strongly typed application ...
Store strongly typed application settings. Contribute to spatie/laravel-settings development by creating an account on GitHub.
usmcgator
usmcgatorOPβ€’2y ago
whoa! that looks like exactly what I need. I'll check it out. Thanks for your help.
Want results from more Discord servers?
Add your server