Custom setting page with filamentphp

I'm attempting to set up the edit page as the primary view for a filamentphp resource, specifically for managing page settings. These settings will consist of a single data row, and I want to restrict it to only being editable, with no option to create new rows or delete the existing one. Despite my efforts to find a solution, I haven't been able to. I'm open to any pointers, workarounds, or alternative approaches that could help me achieve this. Thanks for your assistance!
18 Replies
tuto1902
tuto190211mo ago
Thank you for your question. To get a bit more context, what would be the name of the Model you want the Edit page for? (just trying to get a mental image) is it just Settings?
Obala
Obala11mo ago
Yes it's just Settings
tuto1902
tuto190211mo ago
Ok, so I'll assume the model class name is Settings? When you say there's only a single data row, does that mean that the settings table (associated to the Settings model) only has a single record?
Obala
Obala11mo ago
Yes, it will contain content like website name, contact, address etc, so there can't be multiple rows for it
tuto1902
tuto190211mo ago
Ok. It sounds like you don't really need a whole Resource class to edit the Settings. Sounds more like you just need a custom Page. when you create a custom page with php artisan make:filament-page Settings, it will create a navigation item for you
Obala
Obala11mo ago
Yes can work with page plus livewire component
tuto1902
tuto190211mo ago
The new page itself is a full page livewiere component. And, you can implement the HasForms contract to use Filament's form builder inside this page.
Obala
Obala11mo ago
I managed to make it up to here, but the problem came again filling the form with the database data, I did use fill() method on the form but it prevents me from editing the data since it keeps refreshing with the database data once I start typing
tuto1902
tuto190211mo ago
Interesting...
tuto1902
tuto190211mo ago
Would you mind sharing the code for the Page class you created? you can use three back ticks and then php to share code
public function myCode() {
// Share your code
}
public function myCode() {
// Share your code
}
No description
Obala
Obala11mo ago

public function form(Form $form): Form
{
return $form
->schema([
Section::make('Website setting')
->schema([
TextInput::make('company_name')
->name('Name'),
TextInput::make('company_email')
->name('Email'),
TextInput::make('company_address')
->name('Address'),
])
->columnSpan(2),
Section::make('Meta')
->schema([
FileUpload::make('company_logo')
->name('Logo')
->image()
->maxFiles(1),
FileUpload::make('company_favicon')
->name('Favicon')
->image()
->maxFiles(1),
])
->columnSpan(1),
Section::make('Socials')
->schema([
TextInput::make('facebook_url'),
TextInput::make('twitter_url'),
TextInput::make('linkedin_url')
])
->columnSpan(2),
])
->columns(3)
->statePath('data')
->fill(self::getSetting()->toArray());
}

public function form(Form $form): Form
{
return $form
->schema([
Section::make('Website setting')
->schema([
TextInput::make('company_name')
->name('Name'),
TextInput::make('company_email')
->name('Email'),
TextInput::make('company_address')
->name('Address'),
])
->columnSpan(2),
Section::make('Meta')
->schema([
FileUpload::make('company_logo')
->name('Logo')
->image()
->maxFiles(1),
FileUpload::make('company_favicon')
->name('Favicon')
->image()
->maxFiles(1),
])
->columnSpan(1),
Section::make('Socials')
->schema([
TextInput::make('facebook_url'),
TextInput::make('twitter_url'),
TextInput::make('linkedin_url')
])
->columnSpan(2),
])
->columns(3)
->statePath('data')
->fill(self::getSetting()->toArray());
}
tuto1902
tuto190211mo ago
I think in this case, it would be better to fill the form in the mount() method of the Page class instead of calling the ->fill() method directly in the form schema
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());
}
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());
}
Obala
Obala11mo ago
i did try, but was getting some that I can't use before instantiating the form,
tuto1902
tuto190211mo ago
That's strange. This is how the official documentation recommends adding a form to a component. For our purposes, we can replace Component with Page https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
Obala
Obala11mo ago
I have been referencing the docs trying to solve it but the problem has persisted, maybe I should try updating the filamentphp for now
tuto1902
tuto190211mo ago
That's a good idea.
Obala
Obala11mo ago
Updating filamentphp and using this solved the problem
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());
}
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());
}