F
Filament15mo ago
toeknee

Spatie settings not working

Before I try the spatie settings plugin, can we nest in a single page with for example tabs being the group of settings? i.e. GlobalSettings -> TabForm->Email
18 Replies
Dennis Koch
Dennis Koch15mo ago
Like this? You can just use the Tabs from the form builder
toeknee
toeknee15mo ago
yeah! Do you just build a single settings page with a tab and setting statePath on each tab?
Dennis Koch
Dennis Koch15mo ago
I just store them all as json in options.field_key
toeknee
toeknee15mo ago
Oh, so how do you get them throughout the application?
Dennis Koch
Dennis Koch15mo ago
I basically reuse a Page (which also contains content for frontend) and wrote a small setting() helper which is basically:
function setting($key) {
$settings = once(fn() => \App\Models\Page::find('_settings'));
return json_decode(json_encode(data_get($settings->content, $key)));
}
function setting($key) {
$settings = once(fn() => \App\Models\Page::find('_settings'));
return json_decode(json_encode(data_get($settings->content, $key)));
}
The json_decode(json_encode()) is just for converting array to object structure 😅
toeknee
toeknee15mo ago
Hehe I know that ;-). Just seems a bit labour intensive. Might review it later, but that will do for now. Thanks Dennis! Dennis can I get some of your base examples of for class extends settings and the schema, not quite following how the Filament Spatie Settings plugin full integrates. We need to define the properties onto the settings class, but they cause exceptions everytime I do so. Clearly just missing a linkage somewhere
Dennis Koch
Dennis Koch15mo ago
Sorry, I am not using Spatie Settings. I use a Page model which saves to a JSON file (via Orbit) 😅 I thought the nesting question was about general form nesting, sorry 🙈 But I don't think there should be an issue with nesting. What do you have so far? You have both, right? 1) Spatie Settings class 2) Filament Settings Page
toeknee
toeknee15mo ago
Yeah Tried loading settings 'App\Settings\GlobalSettings', and following properties were missing: emails Whenever I declar the public string within the settings class, I'll just research up somemore on the Spatie Settings class, I was using a different Library but prefer to use Spatie
Dennis Koch
Dennis Koch15mo ago
Can you share the code of both?
toeknee
toeknee15mo ago
Settings Page Class for filament:
<?php

namespace App\Filament\Pages\System;

use App\Settings\GlobalSettings;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\TextInput;
use Filament\Pages\SettingsPage;

class Settings extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog';

protected static string $settings = GlobalSettings::class;

protected static ?string $navigationGroup = 'System';

protected function getFormSchema(): array
{
return [
TextInput::make('emails')
->label('Emails')
->helperText('Comma separated, no space, list of email addresses to receive fleet notifications'),
];
}
}
<?php

namespace App\Filament\Pages\System;

use App\Settings\GlobalSettings;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\TextInput;
use Filament\Pages\SettingsPage;

class Settings extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog';

protected static string $settings = GlobalSettings::class;

protected static ?string $navigationGroup = 'System';

protected function getFormSchema(): array
{
return [
TextInput::make('emails')
->label('Emails')
->helperText('Comma separated, no space, list of email addresses to receive fleet notifications'),
];
}
}
Settings\GlobalSettings.php
<?php

namespace App\Settings;

use Spatie\LaravelSettings\Settings;

class GlobalSettings extends Settings
{
public string $emails;

public static function group(): string
{
return 'System';
}
}
<?php

namespace App\Settings;

use Spatie\LaravelSettings\Settings;

class GlobalSettings extends Settings
{
public string $emails;

public static function group(): string
{
return 'System';
}
}
According to the Filament spatie docs, you need to declare the public variables and then filament settings will pre-populate. Comment out the emails declartion in the settings and it renders as normal in filament, but obviously nothing saves, yet it still says successful heh
Dennis Koch
Dennis Koch15mo ago
Did you run the migration according to spatie docs?
toeknee
toeknee15mo ago
Yeah I've got the database, actually wrote the migration custom as I need to migration so old settings.
Dennis Koch
Dennis Koch15mo ago
What do you mean by "wrote the migration custom"? It's not following the spatie docs? Is the data present in the db?
toeknee
toeknee15mo ago
It's a copy of the spatie migration with some additions, no data exists in the table
Dennis Koch
Dennis Koch15mo ago
I guess that's the issue.
toeknee
toeknee15mo ago
It's not the issue as the table is exactly the same
Dennis Koch
Dennis Koch15mo ago
But is has no data. Check this section in the spatie docs:
toeknee
toeknee15mo ago
Ok I think I'm following now, the fields need adding to the database first I was just assuming they would be added YES!! Thanks a bunch Dennis. It was because no key existed in the database