F
Filamentโ€ข15mo ago
Obala

Edit resource page as the page index

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!
25 Replies
krekas
krekasโ€ข15mo ago
custom page would be the solution i guess
Obala
ObalaOPโ€ข15mo ago
Yes it does work, but the problem comes when I am editing the page, am using ->fill() on the form, when I try to edit it refreshes with the data from the database
krekas
krekasโ€ข15mo ago
I don't get it. If it's only for settings shouldn't it work that way? For example you can check how settings page is done here https://github.com/ploi/roadmap/blob/main/app/Filament/Pages/Settings.php
GitHub
roadmap/app/Filament/Pages/Settings.php at main ยท ploi/roadmap
Open source roadmapping software. Contribute to ploi/roadmap development by creating an account on GitHub.
Obala
ObalaOPโ€ข15mo 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());
}
ModestasV
ModestasVโ€ข15mo ago
That fill should only happen on the mount() function and not on your form. You are always re-filling the form.
Obala
ObalaOPโ€ข15mo ago
Yes, have noticed, now have changed to
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());

//And it works

}
public function mount(): void
{
$this->form->fill($this->getSetting()->toArray());

//And it works

}
But now ```php public function create(Setting $setting): void { $setting->update($this->form->getState()); $setting->save(); //Its just saving new setting row with all the fields to null }
ModestasV
ModestasVโ€ข15mo ago
Upp, you don't need $setting->update(...) and then $setting->save(); Just use the update and it should work ๐Ÿ™‚
Obala
ObalaOPโ€ข15mo ago
its not updating the row still
ModestasV
ModestasVโ€ข15mo ago
Can you try to add:
public function create(Setting $setting): void
{
dd($this->form);
$setting->update($this->form->getState());
}
public function create(Setting $setting): void
{
dd($this->form);
$setting->update($this->form->getState());
}
If that doesn't work, just check what you get from:
$this->data;
$this->data;
Because I used exactly the same approach with:
public function create(): void
{
// ...
$count = count(array_diff($questionsList, $this->data));
public function create(): void
{
// ...
$count = count(array_diff($questionsList, $this->data));
Obala
ObalaOPโ€ข15mo ago
with the dd($this->data); the data is dumped with no issues but $setting->update($this->data); still does not update the row
ModestasV
ModestasVโ€ข15mo ago
dump the $setting variable and see what it returns maybe it's not correctly auto-loaded ๐Ÿ™‚
Obala
ObalaOPโ€ข15mo ago
yes, after dumping $setting from the Setting $setting found it was empty so i change to this
public function create(): void
{
$setting = $this->getSetting();
$setting->update($this->data);
}
public function create(): void
{
$setting = $this->getSetting();
$setting->update($this->data);
}
ModestasV
ModestasVโ€ข15mo ago
๐Ÿ‘
Obala
ObalaOPโ€ข15mo ago
but again its not saving ๐Ÿ˜ซ
ModestasV
ModestasVโ€ข15mo ago
It's not null and you have data? ๐Ÿ™‚ Ps. By checking dd($setting) make sure that it has an ID attached in attrributes
Obala
ObalaOPโ€ข15mo ago
yes its not nulled since the $this->getSetting(); retrieves the first row in the table
ModestasV
ModestasVโ€ข15mo ago
Um... Can you show me your model that is attached to this? I suspect that you have missed $fillable definition there ๐Ÿ™‚
Obala
ObalaOPโ€ข15mo ago
am not using id ๐Ÿ˜† the primary key is set to company_name i have protected $fillable
ModestasV
ModestasVโ€ข15mo ago
well, whatever primary key you are using - if it has it - then it's okay ๐Ÿ˜„ all the fields you are trying to update are there?
Obala
ObalaOPโ€ข15mo ago
yes
ModestasV
ModestasVโ€ข15mo ago
Hmm Double check that the Data isn't wrapped with anything else and has a correct structure ๐Ÿ™‚ Or use saving like this:
$question = Question::first();

$question->fill($this->data);

$question->save();
$question = Question::first();

$question->fill($this->data);

$question->save();
Then you can dd($setting) before the save and see if it was filled correctly
Obala
ObalaOPโ€ข15mo ago
dd() on getSetting()
No description
ModestasV
ModestasVโ€ข15mo ago
check the #attributes and what's inside ๐Ÿ™‚
Obala
ObalaOPโ€ข15mo ago
clear views, route etc and tried this again and it works
public function create(): void
{
$setting = Setting::first();
$setting->fill($this->form->getState());
$setting->save();
}
public function create(): void
{
$setting = Setting::first();
$setting->fill($this->form->getState());
$setting->save();
}
Want results from more Discord servers?
Add your server